A JCheckBox has two states: selected and unselected.
A group of JCheckBoxes is used when we need the user to make multiple 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 JCheckbox.
Create JCheckBox with no label and no image
JCheckBox cb1 = new JCheckBox();
Create JCheckBox with text as "My Choice"
JCheckBox cb2 = new JCheckBox("My Choice");
Create JCheckBox with text as "My Choice" and selected by default
JCheckBox cb3 = new JCheckBox("My Choice", true);
To select/unselect a JCheckBox, to call the 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
Listening to JCheckBox Events with an ActionListener
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; //from w w w. j a v a 2 s . co m import javax.swing.AbstractButton; import javax.swing.JCheckBox; import javax.swing.JFrame; public class Main { public static void main(String args[]) { JFrame frame = new JFrame("Iconizing CheckBox"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JCheckBox aCheckBox4 = new JCheckBox("Stuffed Crust"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { AbstractButton abstractButton = (AbstractButton) actionEvent.getSource(); boolean selected = abstractButton.getModel().isSelected(); System.out.println(selected); // abstractButton.setText(newLabel); } }; aCheckBox4.addActionListener(actionListener); frame.add(aCheckBox4); frame.setSize(300, 200); frame.setVisible(true); } }
Listening to JCheckBox Events with an ItemListener
import java.awt.Color; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; /* w w w . j a v a 2 s . com*/ import javax.swing.AbstractButton; import javax.swing.JCheckBox; import javax.swing.JFrame; public class JCheckBoxItemListener { public static void main(String args[]) { JFrame frame = new JFrame("Iconizing CheckBox"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JCheckBox aCheckBox4 = new JCheckBox("Stuffed Crust"); ItemListener itemListener = new ItemListener() { public void itemStateChanged(ItemEvent itemEvent) { AbstractButton abstractButton = (AbstractButton)itemEvent.getSource(); Color foreground = abstractButton.getForeground(); Color background = abstractButton.getBackground(); int state = itemEvent.getStateChange(); if (state == ItemEvent.SELECTED) { abstractButton.setForeground(background); abstractButton.setBackground(foreground); } } }; aCheckBox4.addItemListener(itemListener); frame.add(aCheckBox4); frame.setSize(300, 200); frame.setVisible(true); } }
Listening to JCheckBox Events with a ChangeListener: listen to armed, pressed, selected, or released state
import javax.swing.AbstractButton; import javax.swing.ButtonModel; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; /* ww w . j av a 2 s. co m*/ public class Main { public static void main(String args[]) { JFrame frame = new JFrame("Iconizing CheckBox"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JCheckBox aCheckBox4 = new JCheckBox("Stuffed Crust"); // Define ChangeListener 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); } }; aCheckBox4.addChangeListener(changeListener); frame.add(aCheckBox4); frame.setSize(300, 200); frame.setVisible(true); } }
The code above generates the following result.