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 typical Swing JCheckbox

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:

    1. Creating a new JCheckBox object
    2. Adding the check box to a container
    3. Setting and getting selected state
    4. Adding event listeners
    5. Customizing appearance
    6. A demo program for JCheckBox
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.

 

1. Creating a new JCheckBox object

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: a simplest checkbox



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", true);


Image: a checkbox with text and selected

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.

 

2. Adding the check box to a container

GridBagConstraints constraints = new GridBagConstraints();
// set constraints...

frame.add(checkbox, constraints); 
 

3. Setting and getting selected state for JCheckBox

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...

} 
 

4. Adding event listeners for JCheckBox

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.

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. 

 

5. Customizing appearance for JCheckBox

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: a check box with mnemonic key

 

6. A demo program for JCheckBox

Following is a demo program that displays three check boxes which allow the user selecting up to three numbers and calculates the sum:

swing jcheckbox demo program

You can download source code and executable jar file for this demo program in the attachments section.

 

Other Java Swing Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Attachments:
Download this file (SwingJCheckBoxDemo.zip)SwingJCheckBoxDemo.zip[Demo program for JCheckBox]9 kB