JCheckBox basic tutorial and examples
- Details
- Written by Nam Ha Minh
- Last Updated on 05 July 2019   |   Print Email
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.- Creating a simplest JCheckBox with only unselected check box without text and icon:
JCheckBox checkbox = new JCheckBox();
Image:
- Creating a JCheckBoxwith only text:
JCheckBox checkbox = new JCheckBox("Enable logging");
Image: - Creating a JCheckBoxwith only icon:
JCheckBox checkbox = new JCheckBox(new ImageIcon("images/mail.png"));
Image:
NOTES: Because the icon replaces the default check box, so using icon for a JCheckBox is not common.
- Creating a JCheckBox with text and selected state:
JCheckBox checkbox = new JCheckBox("Enable logging", true);
Image:
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
- A JCheckBox component can be added to a container like JFrame or JPanel:
frame.add(checkbox); panel.add(checkbox);
- Adding to a frame with BorderLayout:
frame.add(checkbox, BorderLayout.CENTER);
- Adding to a frame with GridBagLayout:
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.- Adding an action listener:
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 } } });
- 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
- Setting tooltip text:
checkbox.setToolTipText("If enabled, write debuggin information to log files.");
Image:
- Setting font style, background color and foreground color:
checkbox.setFont(new java.awt.Font("Arial", Font.BOLD, 14)); checkbox.setBackground(Color.BLUE); checkbox.setForeground(Color.YELLOW);
Image:
- Setting mnemonic key:
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:
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:You can download source code and executable jar file for this demo program in the attachments section.Other Java Swing Tutorials:
- Java Swing Hello World Tutorial for Beginners Using Text Editor
- JFrame basic tutorial and examples
- JPanel basic tutorial and examples
- JLabel basic tutorial and examples
- JTextField basic tutorial and examples
- JComboBox basic tutorial and examples
- JList basic tutorial and examples
- JTree basic tutorial and examples
- A Simple JTable Example for Display
Comments
this code its give my code
diagnostic to I must change the
code. Its give me this:
JCheckBox cannot be resolved
to a type (two times) and
frame cannot be resolved.
Why?
Please send the relevant code so I can help.
im Working on food ordering system My problem is that when i checkbox any of the item text box of that is enable but like i write quantity as "1" in total text field its not showing anything .i am having problem in totaling the cost of items.
Sorry, I don't understand what you mean?