CheckBox

Check boxes are similar to RadioButtons but their selection model is different, because they can flip the selection state between selected and unselected modes. A group of radio buttons, on the other hand, can have only one button selected. Because CheckBox inherits from Button, check boxes have all the usual button characteristics, as discussed in Button. For example, you can specify the image displayed in a check box. Each time the user select a check box (even if it was already selected), it fires an action event, just as in Button.

To create a CheckBox, use:

final CheckBox checkBox = new CheckBox(“Check Box”);

This code produces the CheckBox shown in Figure 1.

Figure: CheckBox Example

To catch select and unselect events, you can try this:

checkBox.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        if(checkBox.isSelected()) {
            System.out.println("CheckBox got selected");
        } else {
            System.out.println("CheckBox got unselected");
        }
    }
});