A JToggleButton is a two-state button. The two states are selected and unselected. The JRadioButton and JCheckBox classes are subclasses of this class. When the user presses the toggle button, it toggles between being pressed or unpressed. JToggleButton is used to select a choice from a list of possible choices. Buttons can be configured, and to some degree controlled, by Actions. Using an Action with a button has many benefits beyond directly configuring a button.
Constructors in JToggleButton:
- JToggleButton(): Creates an initially unselected toggle button without setting the text or image.
- JToggleButton(Action a): Creates a toggle button where properties are taken from the Action supplied.
- JToggleButton(Icon icon): Creates an initially unselected toggle button with the specified image but no text.
- JToggleButton(Icon icon, boolean selected): Creates a toggle button with the specified image and selection state, but no text.
- JToggleButton(String text): Creates an unselected toggle button with the specified text.
- JToggleButton(String text, boolean selected): Creates a toggle button with the specified text and selection state.
- JToggleButton(String text, Icon icon): Creates a toggle button that has the specified text and image, and that is initially unselected.
- JToggleButton(String text, Icon icon, boolean selected): Creates a toggle button with the specified text, image, and selection state.
Commonly Used Methods:
Method |
Description |
getAccessibleContext() |
Gets the AccessibleContext associated with this JToggleButton. |
getUIClassID() |
Returns a string that specifies the name of the l&f class that renders this component. |
paramString() |
Returns a string representation of this JToggleButton. |
updateUI() |
Resets the UI property to a value from the current look and feel. |
Below programs illustrates the JToggleButton class:
1. Java program to implement JToggleButton Events with the ItemListener: In this program, we are creating the frame using JFrame(). Here, setDefaultCloseOperation() is used to set close option for frame. Using JToggleButton() a button is created. Instantiate the ItemListener which contain only itemStateChanged() method that automatically invoked when button is clicked. Event is generated on the button and accordingly, output is printed to the Console. Attach all the Listeners and adding ItemListener to the button. Add button to the frame and setting the size of the frame.
Java
import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
public class JToggleButtonExamp {
public static void main(String args[])
{
JFrame frame = new JFrame( "Selecting Toggle" );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JToggleButton toggleButton = new JToggleButton( "Toggle Button" );
ItemListener itemListener = new ItemListener() {
public void itemStateChanged(ItemEvent itemEvent)
{
int state = itemEvent.getStateChange();
if (state == ItemEvent.SELECTED) {
System.out.println( "Selected" );
}
else {
System.out.println( "Deselected" );
}
}
};
toggleButton.addItemListener(itemListener);
frame.add(toggleButton, BorderLayout.NORTH);
frame.setSize( 300 , 125 );
frame.setVisible( true );
}
}
|
Output:
2. Java program to implement JToggleButton Event using ActionListener: Here, a JToggleButton is created on the JFrame. Then, we define the ActionListener. actionPerformed() is the only method in ActionListener() which is invoked whenever a registered component is clicked. abstractButton.getModel().isSelected() returns true if button is selected else return false. Attach the Listener to the ToggleButton.
Java
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
public class JToggleButtonExamp {
public static void main(String args[])
{
JFrame frame = new JFrame( "Selecting Toggle" );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JToggleButton toggleButton = new JToggleButton( "Toggle Button" );
ActionListener actionListener = new ActionListener()
{
public void actionPerformed(ActionEvent actionEvent)
{
AbstractButton abstractButton =
(AbstractButton)actionEvent.getSource();
boolean selected = abstractButton.getModel().isSelected();
System.out.println( "Action - selected=" + selected + "\n" );
}
};
toggleButton.addActionListener(actionListener);
frame.add(toggleButton, BorderLayout.NORTH);
frame.setSize( 300 , 125 );
frame.setVisible( true );
}
}
|
Output :
Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javase/7/docs/api/javax/swing/JToggleButton.html
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
05 Jan, 2023
Like Article
Save Article