Home » Java programming language

Java AWT CheckboxGroup

Java | AWT CheckboxGroup: In this tutorial, we will look at one of the Java AWT components, the AWT CheckboxGroup with example.
Submitted by Saranjay Kumar, on April 29, 2020

We have seen in previous sections what a Checkbox class is. When we have a group of checkboxes, we can select as many as we want from them, However, in case we want to restrict the user so that he can choose a maximum of 1 option from a set, we use the CheckboxGroup class. It is synonymous with the more frequently used Radio Button.

The AWT package does not have the option to render a radio button. Thus a CheckboxGroup object is used.

A CheckboxGroup object is used to form a set of checkboxes, out of which only one can be in the on state and all others must be in the off state.

A CheckboxGroup object creates an ItemEvent whenever the state of one of the checkboxes changes. We can use the ItemListener interface to handle events fired by this class. We shall learn more about dealing with events in later sections.

Consider the following code -

import java.awt.*;
import javax.swing.*;

public class CreateCheckboxGroup {

    CreateCheckboxGroup() {
        Frame f = new Frame();

        CheckboxGroup fruits = new CheckboxGroup();
        CheckboxGroup cars = new CheckboxGroup();

        Checkbox c1 = new Checkbox("Apple", fruits, false);
        Checkbox c2 = new Checkbox("Mango", fruits, false);
        Checkbox c3 = new Checkbox("Banana", fruits, true);

        Checkbox c4 = new Checkbox("BMW", cars, false);
        Checkbox c5 = new Checkbox("Merc", cars, false);

        f.setLayout(new BoxLayout(f, BoxLayout.Y_AXIS));
        f.setVisible(true);
        f.setSize(300, 300);

        f.add(c1);
        f.add(c2);
        f.add(c3);
        f.add(c4);
        f.add(c5);
    }

    public static void main(String[] args) {
        CreateCheckboxGroup ob = new CreateCheckboxGroup();
    }
}

Output

Java AWT CheckboxGroup

Java AWT CheckboxGroup

In the output, the first picture depicts the initial view of the application. As can be seen, banana is selected from one group and no choice has been selected from the other group. The second picture depicts the state after the user interacts with the application, by selecting Mango from fruits and Merc from cars.

In the code, we have created two objects of the CheckboxGroup class. These two sets, namely "fruits" and "cars" hold 3 and 2 checkboxes respectively. Each checkbox interacts with the user in the same way as any usual Checkbox class object. However, the key difference being that only one checkbox can be selected to be true from a set. Whenever the user changes his option, all other checkboxes get automatically unchecked.

In the "fruits" set, we have set c3 object to be checked by default. This means that all other checkboxes of this group will be unchecked initially, except for this checkbox.

In the "cars" set, we have created two checkboxes, c4 and c5. Both objects are unchecked by default. The user has the ability to choose any one option from these two and also the freedom to change his selected option at any point in time.

To add any checkbox to a checkbox group, we use the constructor as shown in the code.
Checkbox(String name, CheckboxGroup cbg, boolean state)




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.