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.
JCheckBox checkbox = new JCheckBox();
Image:
JCheckBox checkbox = new JCheckBox("Enable logging");
JCheckBox checkbox = new JCheckBox(new ImageIcon("images/mail.png"));
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.
frame.add(checkbox); panel.add(checkbox);
frame.add(checkbox, BorderLayout.CENTER);
GridBagConstraints constraints = new GridBagConstraints(); // set constraints... frame.add(checkbox, constraints);
checkbox.setSelected(true); checkbox.setSelected(false);Getting state of the check box:
if (checkbox.isSelected()) { // selected, do something... } else { // un-selected, do something else... }
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: