In this tutorial

You will learn how to...
  • How to create a checkbox.
  • How to select a checkbox.
  • How to find if a checkbox is selected.
  • How to add an ItemListener to it.

JCheckbox - Part 1

The JCheckbox is a widget that lets you select more than one attribute at a time on the screen by 'checking' i.e. ticking selections in a list. This is useful when multiple choices are involved, like the people you want to play in your football team. Each CheckBox has a state of either selected or deselected.

Let's have a look at an example.
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import javax.swing.*;
import java.awt.*;
import java.awt.Color;

public class CheckBoxExample{
    
    JCheckBox redCB, blueCB, greenCB, yellowCB;

    public JPanel createContentPane (){
        
        JPanel totalGUI = new JPanel();
        
        // We create four checkboxes to control what is currently on-screen.
        // At the start, we set the red checkbox to 'ticked' or selected.
        
        JPanel checkBoxPanel = new JPanel();
        checkBoxPanel.setLayout(new BoxLayout(checkBoxPanel, BoxLayout.PAGE_AXIS));
        checkBoxPanel.add(Box.createRigidArea(new Dimension(10,0)));

        redCB = new JCheckBox("Red");
        redCB.setSelected(true);
        
        checkBoxPanel.add(redCB);
        checkBoxPanel.add(Box.createHorizontalGlue());
        
        blueCB = new JCheckBox("Blue");
        checkBoxPanel.add(blueCB);
        checkBoxPanel.add(Box.createHorizontalGlue());
        
        greenCB = new JCheckBox("Green");
        checkBoxPanel.add(greenCB);
        checkBoxPanel.add(Box.createHorizontalGlue());
        
        yellowCB = new JCheckBox("Yellow");
        checkBoxPanel.add(yellowCB);
        checkBoxPanel.add(Box.createRigidArea(new Dimension(10, 0)));
        
        // Now we create a simple JPanel that displays our four coloured boxes.

        JPanel boxPanel = new JPanel(new GridLayout(2, 2, 20, 20));
        
        JPanel redBox = createSquareJPanel(Color.red, 50);
        JPanel blueBox = createSquareJPanel(Color.blue, 50);
        JPanel greenBox = createSquareJPanel(Color.green, 50);
        JPanel yellowBox = createSquareJPanel(Color.yellow, 50);
        
        // This sets all bar the red box to be hidden.
  
        blueBox.setVisible(false);
        greenBox.setVisible(false);
        yellowBox.setVisible(false);
        
        boxPanel.add(redBox);
        boxPanel.add(blueBox);
        boxPanel.add(greenBox);
        boxPanel.add(yellowBox);
        
        totalGUI.add(checkBoxPanel);
        totalGUI.add(boxPanel);
        
        totalGUI.setOpaque(true);
        return totalGUI;
    }

    // In this method, we create a square JPanel of a colour and set size
    // specified by the arguments.

    private JPanel createSquareJPanel(Color color, int size) {
        JPanel tempPanel = new JPanel();
        tempPanel.setBackground(color);
        tempPanel.setMinimumSize(new Dimension(size, size));
        tempPanel.setMaximumSize(new Dimension(size, size));
        tempPanel.setPreferredSize(new Dimension(size, size));
        return tempPanel;
    }

    private static void createAndShowGUI() {

        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("[=] JCheckBox [=]");

        CheckBoxExample demo = new CheckBoxExample();
        frame.setContentPane(demo.createContentPane());
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
When run, it turns out like this.
Picture of a Checkbox

You can see the four checkboxes running down the left-hand side of the GUI. If you run the program, you can click on each which will select then deselect the checkbox. Each checkbox is independent, you can have as many selected or deselected as you like.

You may have noticed when the red checkbox is deselected the red panel does not disappear, and when any of the other checkboxes are selected nothing happens. This is because they are missing an event listener that we will look at after going through this example.

To instantiate a JCheckbox is easy. The String passed as an argument is a label created automatically beside the checkbox.
20
        redCB = new JCheckBox("Red");
The next line sets the checkbox to initially be selected when the GUI is started up.
21
        redCB.setSelected(true);
That's about it, there is nothing really special to a Checkbox. We need to make it and the GUI come alive by using a event listener called an ItemListener.

Back Top Next
Email Me
Code Style


Required Lessons

External Links

Created and Edited by Stuart Davidson
All Rights Reserved ©

Valid XHTML 1.0 Strict