A JToggleButton is a two-state button. The two states are selected and unselected.
When we press the toggle button, it toggles between being depressed and undepressed.
A JButton is used to initiate an action whereas a JToggleButton is used to select a choice from a list of possible choices.
Typically, we use a group of JToggleButtons to let the user select one choice from a list of mutually exclusive choices.
We can use a combination of an Action object, a string label, an icon, and a boolean flag to indicate if it is selected by default to create JToggleButton.
Create a JToggleButton with no label and no image
JToggleButton tb1 = new JToggleButton();
Create JToggleButton with text as "My Choice"
JToggleButton tb2 = new JToggleButton("My Choice");
Create JToggleButton with text as "My Choice" and selected by default
JToggleButton tb3 = new JToggleButton("My Choice", true);
To select/unselect a JToggleButton, to call setSelected() methods.
To check if it is selected, use their isSelected() methods.
The following code shows how to use these methods:
tb3.setSelected(true); // Select tb3 boolean b1 = tb3.isSelected(); // will store true in b1 tb3.setSelected(false); // Unselect tb3 boolean b2 = tb3.isSelected(); // will store false in b2
If the selection is mutually exclusive, we must group all your choices in a button group. An instance of the ButtonGroup class represents a button group.
We can add and remove a JToggleButton to a button group by using its add() and remove() methods, respectively.
Initially all members of a button group are unselected.
To form a button group, we need to add all mutually exclusive choice components to an object of the ButtonGroup class. we do not add a ButtonGroup object to a container.
We must add all choice components to the container.
The following code shows how to listen to JToggleButton Events with an ActionListener.
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /*from ww w . j a v a2s . c o m*/ import javax.swing.AbstractButton; import javax.swing.JFrame; import javax.swing.JToggleButton; public class Main { public static void main(String args[]) { JFrame frame = new JFrame("Selecting Toggle"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JToggleButton toggleButton = new JToggleButton("Toggle Button"); // Define ActionListener 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"); } }; // Attach Listeners toggleButton.addActionListener(actionListener); frame.add(toggleButton, BorderLayout.NORTH); frame.setSize(300, 125); frame.setVisible(true); } }
The following code shows how to listen to JToggleButton Events with an ItemListener.
import java.awt.BorderLayout; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; // ww w. j a va 2 s. c om import javax.swing.JFrame; import javax.swing.JToggleButton; public class Main { 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"); } } }; // Attach Listeners toggleButton.addItemListener(itemListener); frame.add(toggleButton, BorderLayout.NORTH); frame.setSize(300, 125); frame.setVisible(true); } }
The following code shows how to listen to JToggleButton Events with a ChangeListener. It listens to the changes for its armed, pressed, and selected properties.
/*from w ww . j a va 2 s.c om*/ import java.awt.BorderLayout; import javax.swing.AbstractButton; import javax.swing.ButtonModel; import javax.swing.JFrame; import javax.swing.JToggleButton; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class Main { public static void main(String args[]) { JFrame frame = new JFrame("Selecting Toggle"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JToggleButton toggleButton = new JToggleButton("Toggle Button"); ChangeListener changeListener = new ChangeListener() { public void stateChanged(ChangeEvent changeEvent) { AbstractButton abstractButton = (AbstractButton) changeEvent.getSource(); ButtonModel buttonModel = abstractButton.getModel(); boolean armed = buttonModel.isArmed(); boolean pressed = buttonModel.isPressed(); boolean selected = buttonModel.isSelected(); System.out.println("Changed: " + armed + "/" + pressed + "/" + selected); } }; // Attach Listeners toggleButton.addChangeListener(changeListener); frame.add(toggleButton, BorderLayout.NORTH); frame.setSize(300, 125); frame.setVisible(true); } }