Checkbox groups in Java AWT

Java AWT (Abstract Window Toolkit) provides various components for creating graphical user interfaces (GUIs). One of these components is the CheckboxGroup class, which allows you to group multiple checkboxes together so that only one checkbox in the group can be selected at a time.

Creating a CheckboxGroup

To create a CheckboxGroup in Java AWT, you need to instantiate the CheckboxGroup class:

CheckboxGroup group = new CheckboxGroup();

Adding Checkboxes to the Group

Once you have created a CheckboxGroup, you can create individual checkboxes and add them to the group. By default, the first checkbox added to the group is initially selected. Here’s an example of how to add checkboxes to the group:

Checkbox checkbox1 = new Checkbox("Option 1", group, true);
Checkbox checkbox2 = new Checkbox("Option 2", group, false);
Checkbox checkbox3 = new Checkbox("Option 3", group, false);

In the example above, checkbox1 is initially selected because it is added first and has the true parameter. checkbox2 and checkbox3 are initially unselected.

Handling Checkbox Selection

To handle the checkbox selection, you can register an ActionListener or ItemListener to each checkbox in the group. This allows you to perform specific actions whenever a checkbox is selected or deselected. Here’s an example:

checkbox1.addItemListener(new ItemListener() {
    @Override
    public void itemStateChanged(ItemEvent e) {
        if (checkbox1.getState()) {
            // Checkbox 1 is selected
        } else {
            // Checkbox 1 is deselected
        }
    }
});

checkbox2.addItemListener(new ItemListener() {
    @Override
    public void itemStateChanged(ItemEvent e) {
        if (checkbox2.getState()) {
            // Checkbox 2 is selected
        } else {
            // Checkbox 2 is deselected
        }
    }
});

checkbox3.addItemListener(new ItemListener() {
    @Override
    public void itemStateChanged(ItemEvent e) {
        if (checkbox3.getState()) {
            // Checkbox 3 is selected
        } else {
            // Checkbox 3 is deselected
        }
    }
});

In the above example, each checkbox has its own ItemListener which is triggered when the state of the checkbox changes.

Conclusion

Checkbox groups in Java AWT provide a way to group multiple checkboxes together and allow only one checkbox to be selected at a time. By utilizing the CheckboxGroup class and handling the checkbox selection events, you can create interactive GUIs in Java.