1. About JCheckBox
Like JToggleButton, JCheckBox is also a two-state information component. It can represent two states. One is Checked State and other one is UnChecked State. In a checked state, the user will see a tick mark and in an unchecked state, there will not be any tick mark. Java Swing check box will produce ItemEvent when user check or uncheck a check box.
2. About JCheckBox Example
The Example we create here is shown in the below picture:

The example contains three JCheckBox items in the content pane of the JFrame window. It also has a JTextField element. When the user places a checkmark on any of the available checkbox, the JTextField will report that. In the above picture, the text field reports, user placed a check mark on the check box item 2. Now, we will implement this example.
3. Class Data Members
We have a JTextField
data members in our class. This member will report the lastly checked checkbox and we will access it while we handle the user action on the check boxes.
1 2 |
//Sample 01: Required Class members JTextField tf; |
4. Create JCheckBox Components
In code snippet 2, we create three JCheckBoxes
and we give the checkbox label as string while we construct it. Next, the our snippet creates JTextField
with a column width of 35 Characters. After creating these components, we add it to the content pane of the JFrame via the add
method.
1 2 3 4 5 6 7 8 9 10 11 |
//Sample 02: Create Check Boxes JCheckBox chk1 = new JCheckBox("Check Box 1"); JCheckBox chk2 = new JCheckBox("Check Box 2"); JCheckBox chk3 = new JCheckBox("Check Box 3"); tf = new JTextField(35); //Sample 03: Add to Content Pane ControlHost.add(chk1); ControlHost.add(chk2); ControlHost.add(chk3); ControlHost.add(tf); |
5. Report Checked CheckBox via ItemListener
The
JCheckBox reports the State-Change to the
itemStateChanged event handler. We use this handler to check the state of the Checkbox. Note, since we provide an anonymous handler for each check boxes, we no need to find the event source. JCheckBox method
isSelected returns true when the user changes the checkbox state from Un-Ticked to Ticked. We use this method and update the JTextField to state which JCheckBox
was recently moved to the checked state.
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 |
//Sample 04: Handle Check Box UI Action chk1.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if (chk1.isSelected()) { tf.setText("Check Box 1 - Selected"); } } }); chk2.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if (chk2.isSelected()) { tf.setText("Check Box 2 - Selected"); } } }); chk3.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if (chk3.isSelected()) { tf.setText("Check Box 3 - Selected"); } } }); |
You can watch this complete example as a Youtube video below.
6. Watch JCheckBox Demo – Youtube
7. Code Reference
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 JCheckBoxDemo frame = new JCheckBoxDemo("JCheckBox Example"); frame.setVisible(true); } } |
JCheckBoxDemo.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 59 60 61 62 63 64 |
package tube.coding.examples; import java.awt.Container; import java.awt.FlowLayout; import java.awt.HeadlessException; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JTextField; public class JCheckBoxDemo extends JFrame { //Sample 01: Required Class members String ChkString = "Selected Check Box List: "; JTextField tf; public JCheckBoxDemo(String title) throws HeadlessException { super(title); setBounds(100, 100, 450, 110); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); //Sample 02: Create Check Boxes JCheckBox chk1 = new JCheckBox("Check Box 1"); JCheckBox chk2 = new JCheckBox("Check Box 2"); JCheckBox chk3 = new JCheckBox("Check Box 3"); tf = new JTextField(35); //Sample 03: Add to Content Pane ControlHost.add(chk1); ControlHost.add(chk2); ControlHost.add(chk3); ControlHost.add(tf); //Sample 04: Handle Check Box UI Action chk1.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if (chk1.isSelected()) { tf.setText("Check Box 1 - Selected"); } } }); chk2.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if (chk2.isSelected()) { tf.setText("Check Box 2 - Selected"); } } }); chk3.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if (chk3.isSelected()) { tf.setText("Check Box 3 - Selected"); } } }); } } |
Categories: Swing
Tags: isSelected, ItemListener, itemStateChanged, JCheckBox