Creating a JCheckbox Component

By default, the state of the checkbox is off; to change the state, see Getting and Setting the State of a JCheckbox Component. Also by default, the checkbox is left-justified and vertically centered. The methods to control the text alignment are identical to those of a JButton; see Moving the Label/Icon Pair in a JButton Component.
// Create an action Action action = new AbstractAction("CheckBox Label") { // This method is called when the button is pressed public void actionPerformed(ActionEvent evt) { // Perform action JCheckBox cb = (JCheckBox)evt.getSource(); // Determine status boolean isSel = cb.isSelected(); if (isSel) { // The checkbox is now selected } else { // The checkbox is now deselected } } }; // Create the checkbox JCheckBox checkBox = new JCheckBox(action);

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.