Open In App

Java AWT CheckboxGroup

Java AWT CheckboxGroup is a class that is used to create checkboxes and group a set of checkbox buttons together. Many components in the CheckboxGroup separate, which serves as a means to group a set of checkboxes. In this clause, we will delve into the CheckboxGroup class, and its methods, and demo its usage through a very simple example.

Class Declaration of CheckboxGroup

The CheckboxGroup class is declared as follows:



public class CheckboxGroup extends Object implements Serializable

Here, it extends the Object class and implements the Serializable interface.

Class Constructors

The CheckboxGroup class has a single constructor:



CheckboxGroup()

This constructor creates a new instance of CheckboxGroup.

Class Methods in Java AWT CheckboxGroup

Method

Description

Checkbox getCurrent()

(Deprecated): As of JDK version 1.1, this method is deprecated and replaced by getSelectedCheckbox()

Checkbox getSelectedCheckbox()

Checkbox getSelectedCheckbox()

Checkbox getSelectedCheckbox()

(Deprecated): As of JDK version 1.1, this method is deprecated and replaced by setSelectedCheckbox(Checkbox)

void setSelectedCheckbox(Checkbox box)

Sets the currently selected checkbox in this group to be the specified checkbox.

String toString()

Returns a string representation of this checkbox group, including the value of its current selection.

Example of AWT CheckboxGroup with ItemListener




// Java Program to implement AWT 
// CheckboxGroup with ItemListener
import java.awt.*;
import java.awt.event.*;
  
// Driver Class
public class GeeksCheckboxGroupDemo {
    // Components for the GUI
    private Frame mainFrame;
    private Label headerLabel;
    private Label statusLabel;
    private Panel controlPanel;
  
    // Constructor to set up the GUI
    public GeeksCheckboxGroupDemo() {
        prepareGUI();
    }
  
    // Method to set up the basic structure of the GUI
    private void prepareGUI() {
        // Create the main frame
        mainFrame = new Frame("GeeksforGeeks AWT Examples");
        mainFrame.setSize(400, 400);
        mainFrame.setLayout(new GridLayout(3, 1));
        mainFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowEvent) {
                System.exit(0);
            }
        });
  
        // Create labels for header and status
        headerLabel = new Label();
        headerLabel.setAlignment(Label.CENTER);
  
        statusLabel = new Label();
        statusLabel.setAlignment(Label.CENTER);
        statusLabel.setSize(350, 100);
  
        // Create a panel for controls
        controlPanel = new Panel();
        controlPanel.setLayout(new FlowLayout());
  
        // Add components to the frame
        mainFrame.add(headerLabel);
        mainFrame.add(controlPanel);
        mainFrame.add(statusLabel);
        mainFrame.setVisible(true);
    }
  
    // Method to show the CheckboxGroup demo
    private void showCheckboxGroupDemo() {
        // Set the header label
        headerLabel.setText("Control in action: GeeksCheckboxGroup");
  
        // Create a CheckboxGroup
        CheckboxGroup categoryGroup = new CheckboxGroup();
  
        // Create checkboxes with different names
        Checkbox chkJava = new Checkbox("Java", categoryGroup, true);
        Checkbox chkPython = new Checkbox("Python", categoryGroup, false);
        Checkbox chkCSharp = new Checkbox("C#", categoryGroup, false);
  
        // Add item listeners to each checkbox
        chkJava.addItemListener(getCheckboxItemListener("Java"));
        chkPython.addItemListener(getCheckboxItemListener("Python"));
        chkCSharp.addItemListener(getCheckboxItemListener("C#"));
  
        // Add checkboxes to the control panel
        controlPanel.add(chkJava);
        controlPanel.add(chkPython);
        controlPanel.add(chkCSharp);
  
        // Display the frame
        mainFrame.setVisible(true);
    }
  
    // Method to create an ItemListener for a given category
    private ItemListener getCheckboxItemListener(final String categoryName) {
        // Return an ItemListener for the given category
        return new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    statusLabel.setText(categoryName + " Checkbox: checked");
                } else {
                    statusLabel.setText(categoryName + " Checkbox: unchecked");
                }
            }
        };
    }
  
    // Main method to create an instance and show the demo
    public static void main(String[] args) {
        // Create an instance of GeeksCheckboxGroupDemo and show the demo
        GeeksCheckboxGroupDemo checkboxGroupDemo = new GeeksCheckboxGroupDemo();
        checkboxGroupDemo.showCheckboxGroupDemo();
    }
}

Output:

that


Article Tags :