Java Swing | JToggleButton Class
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:
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
- 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.
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.
Note: The above programs might not run in an online IDE. Please use an offline compiler.