1. JToggleButton Introduction
In Java Swing, the JToggleButton is a two-state button. Here, the two states are On and Off. When the user clicks the button, it toggles between this ON & OFF state. JToggleButton component is the base class for the JCheckBox and JRadioButton. Hence, the JToggleButton will raise an ItemEvent which we can handle via the ItemListener.
2. About The Example
The below picture shows the Java Swing’s JToggleButton (Two-State Button) Example:

The example contains three components. One is a JLabel which tells in what mode the example is in. The button labelled OFF is the JToggleButton and it will switch the state between Selected & Unselected. The text box tells what kind of the state change is made by the user while he/she clicked the Two-State button last time.
3. Create JToggleButton as Two-State Button
At code snippet 2, we create JTextField with 30-character width and then we create JLabel. The JLabel will show the initial text “Write Mode”. We will change this text based on the JToggleButton state. In code snippet 3, we create the JToggleButton and to the constructor we pass the string “ON” and Java Swing will display this when the frame is displayed for the first time to the user. Finally, we add all these three components to the JFrame which is managed by the FlowLayout.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
//Sample 01: Set Size and Position setBounds(100, 100, 400, 100); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); //Sample 02: Create a Text Field JTextField txt1 = new JTextField(30); JLabel lbl = new JLabel("Write Mode:"); //Sample 03: Create Toggle Button JToggleButton tgBtn = new JToggleButton("ON"); //Sample 04: Add Controls to Content Pane add(lbl); add(tgBtn); add(txt1); |
4. Handle ItemEvent of Two-State Button
The JToggleButton
will raise an
ItemEvent when the user clicks it. Method
itemStateChanged of the ItemListener
will handle this event and in the below code we use the anonymous inner class and provide the handler function implementation. The method
isSelected of the JToggleButton tells whether it is in selected state (ON) or not.
In our handler function, we use this isSelected
function and its return value to form the if condition. When the JToggleButton is in the selected state, we set the button text as “OFF” and when the button is not in the selected state we set the button text as “ON”. Also note, we report the item state change event in the text box as well.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
//Sample 05: Handle the Toggle Button Event tgBtn.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if(tgBtn.isSelected()) { tgBtn.setText("OFF"); txt1.setText("Mode Changed from Write to Read"); } else { tgBtn.setText("ON"); txt1.setText("Mode Changed from Read to Write"); } } }); |
5. Youtube Demo
6. Code Reference
6.1 MainEntry.java
1 2 3 4 5 6 7 8 9 |
package tube.coding.examples; public class MainEntry { public static void main(String[] args) { //Sample 07: Create Instance of JFrameDemo JToggleButtonExample frame = new JToggleButtonExample("JToggleButton Example"); frame.setVisible(true); } } |
6.2 JToggleButtonExample.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
package tube.coding.examples; import java.awt.Container; import java.awt.FlowLayout; import java.awt.HeadlessException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.JToggleButton; import javax.swing.SwingConstants; public class JToggleButtonExample extends JFrame { public JToggleButtonExample(String title) throws HeadlessException { super(title); //Sample 01: Set Size and Position setBounds(100, 100, 400, 100); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); //Sample 02: Create a Text Field JTextField txt1 = new JTextField(30); JLabel lbl = new JLabel("Write Mode:"); //Sample 03: Create Toggle Button JToggleButton tgBtn = new JToggleButton("ON"); //Sample 04: Add Controls to Content Pane add(lbl); add(tgBtn); add(txt1); //Sample 05: Handle the Toggle Button Event tgBtn.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if(tgBtn.isSelected()) { tgBtn.setText("OFF"); txt1.setText("Mode Changed from Write to Read"); } else { tgBtn.setText("ON"); txt1.setText("Mode Changed from Read to Write"); } } }); } } |
Categories: Swing
Tags: isSelected, ItemEvent, itemStateChanged, JToggleButton