JCheckBox is a Swing component that represents an item which shows a state of selected or unselected. User can change this state by clicking on the check box of the component. Here is a typical JCheckBox component in default Java look and feel:
A standard JCheckBox component contains a check box and a label that describes purpose of the check box. An icon and mnemonic key also can be set for this component.
This article describes how to use JCheckBox component in Swing applications, including code examples, common practices and a demo program.
Table of content:
The following code snippet shows a typical usage of JCheckBox component:
JCheckBox checkbox = new JCheckBox("Enable logging"); // add to a container frame.add(checkbox); // set state checkbox.setSelected(true); // check state if (checkbox.isSelected()) { // do something... } else { // do something else... }
Let’s take a closer look at the common practices and examples.
According to Javadoc, the JCheckBox class extends from AbstractButton and JToggleButton classes so it has all characteristics of common button as well as two-state (toggle) button.
JCheckBox checkbox = new JCheckBox();
Image:
When creating new objects of the JCheckBox class, we can specify text, icon, and selected state in any combination of them. Here are some examples:
JCheckBox checkbox = new JCheckBox("Enable logging");
JCheckBox checkbox = new JCheckBox(new ImageIcon("images/mail.png"));
NOTES: Because the icon replaces the default check box, so using icon for a JCheckBox is not common.
JCheckBox checkbox = new JCheckBox("Enable logging", true);
Image:
The text, icon and selected state can be set later, for example:
JCheckBox checkbox = new JCheckBox(); checkbox.setText("Enable logging"); checkbox.setSelected(true); checkbox.setIcon(icon);
We can also pass an action class to the JCheckBox’s constructor to handle its state and clicking event:
JCheckBox checkbox = new JCheckBox(new CheckboxAction("Enable logging"));
Code of the action class:
class CheckboxAction extends AbstractAction { public CheckboxAction(String text) { super(text); } @Override public void actionPerformed(ActionEvent e) { JCheckBox cbLog = (JCheckBox) e.getSource(); if (cbLog.isSelected()) { System.out.println("Logging is enabled"); } else { System.out.println("Logging is disabled"); } } }
Using an action class would be useful in case we have a group of items that does the same action, such as menu item, button and check box.
frame.add(checkbox); panel.add(checkbox);
frame.add(checkbox, BorderLayout.CENTER);
GridBagConstraints constraints = new GridBagConstraints(); // set constraints... frame.add(checkbox, constraints);
Setting state for the check box:
checkbox.setSelected(true); checkbox.setSelected(false);
Getting state of the check box:
if (checkbox.isSelected()) { // selected, do something... } else { // un-selected, do something else... }
The most interested action of the check box is the clicking event. We can specify a handler for the check box’s clicking event either by adding an action listener or setting an action handler.
checkbox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { JCheckBox cb = (JCheckBox) event.getSource(); if (cb.isSelected()) { // do something if check box is selected } else { // check box is unselected, do something else } } });
AbstractAction actionHandler = new CheckboxAction("Enable logging"); checkbox.setAction(actionHandler);
Code of the action handler class:
class CheckboxAction extends AbstractAction { public CheckboxAction(String text) { super(text); } @Override public void actionPerformed(ActionEvent event) { JCheckBox cbLog = (JCheckBox) event.getSource(); if (cbLog.isSelected()) { System.out.println("Logging is enabled"); } else { System.out.println("Logging is disabled"); } } }
If we have a group of check boxes and want to use only one action listener for all check boxes:
JCheckBox checkboxOne = new JCheckBox("One"); JCheckBox checkboxTwo = new JCheckBox("Two"); JCheckBox checkboxThree = new JCheckBox("Three"); // add these check boxes to the container... // add an action listener ActionListener actionListener = new ActionHandler(); checkboxOne.addActionListener(actionListener); checkboxTwo.addActionListener(actionListener); checkboxThree.addActionListener(actionListener); // code of the action listener class class ActionHandler implements ActionListener { @Override public void actionPerformed(ActionEvent event) { JCheckBox checkbox = (JCheckBox) event.getSource(); if (checkbox == checkboxOne) { System.out.println("Checkbox #1 is clicked"); } else if (checkbox == checkboxTwo) { System.out.println("Checkbox #2 is clicked"); } else if (checkbox == checkboxThree) { System.out.println("Checkbox #3 is clicked"); } } }
NOTES: Do not use one action (the class that extends from AbstractActionclass) for a group of check boxes, because the action class will set same state and attributes for all the check boxes. So using an action listener is the recommended practice.
checkbox.setToolTipText("If enabled, write debuggin information to log files.");
checkbox.setFont(new java.awt.Font("Arial", Font.BOLD, 14)); checkbox.setBackground(Color.BLUE); checkbox.setForeground(Color.YELLOW);
checkbox.setMnemonic('E');
That makes the letter E in the text underlined, so the user can check/uncheck this check box by pressing Alt + E.
Image:
Following is a demo program that displays three check boxes which allow the user selecting up to three numbers and calculates the sum:
You can download source code and executable jar file for this demo program in the attachments section.