In Java AWT, a CheckboxGroup is a class that provides a way to group several Checkbox components together. When Checkbox components are grouped using a CheckboxGroup, only one Checkbox in the group can be selected at a time.
Here’s an example of how to use CheckboxGroup in Java AWT:
import java.awt.*; import java.awt.event.*; public class CheckboxGroupExample extends Frame implements ItemListener { CheckboxGroup checkboxGroup; Checkbox checkbox1, checkbox2, checkbox3; public CheckboxGroupExample() { setLayout(new FlowLayout()); checkboxGroup = new CheckboxGroup(); checkbox1 = new Checkbox("Checkbox 1", checkboxGroup, false); checkbox1.addItemListener(this); add(checkbox1); checkbox2 = new Checkbox("Checkbox 2", checkboxGroup, false); checkbox2.addItemListener(this); add(checkbox2); checkbox3 = new Checkbox("Checkbox 3", checkboxGroup, false); checkbox3.addItemListener(this); add(checkbox3); setSize(300, 200); setVisible(true); } public void itemStateChanged(ItemEvent e) { Checkbox selectedCheckbox = checkboxGroup.getSelectedCheckbox(); if (selectedCheckbox != null) { System.out.println("Selected Checkbox: " + selectedCheckbox.getLabel()); } } public static void main(String[] args) { new CheckboxGroupExample(); } }
In this example, we create a CheckboxGroup called checkboxGroup
. We then create three Checkbox components (checkbox1
, checkbox2
, and checkbox3
) and add them to the Frame. We pass checkboxGroup
as the second argument when creating each Checkbox, which groups the three Checkboxes together.
We also implement the ItemListener
interface and override the itemStateChanged()
method, which is called when a Checkbox’s state changes. In this method, we get the selected Checkbox from the CheckboxGroup using the getSelectedCheckbox()
method, and print its label to the console.
When we run this example and click on any of the Checkboxes, only one Checkbox in the group can be selected at a time, and the label of the selected Checkbox is printed to the console.
AWT CheckboxGroup Class Declaration:
The CheckboxGroup class in Java AWT is declared as follows:
public class CheckboxGroup implements Serializable
The CheckboxGroup
class is a public class in the java.awt
package. It implements the Serializable
interface, which means that instances of the class can be serialized (i.e., converted to a byte stream for storage or transmission) and deserialized (i.e., converted back to an object).
The CheckboxGroup
class provides a way to group several Checkbox components together, so that only one Checkbox in the group can be selected at a time. The CheckboxGroup class contains a single method called getSelectedCheckbox()
, which returns the Checkbox in the group that is currently selected.
The CheckboxGroup class does not provide any constructors. Instead, a CheckboxGroup object is created by passing it as an argument to the constructor of a Checkbox component, which specifies that the Checkbox belongs to that CheckboxGroup.
Java AWT CheckboxGroup Example:
Here’s an example of using CheckboxGroup in Java AWT to create a simple user interface with grouped checkboxes:
import java.awt.*; import java.awt.event.*; public class CheckboxGroupExample extends Frame implements ItemListener { CheckboxGroup group; Checkbox checkbox1, checkbox2, checkbox3; public CheckboxGroupExample() { setLayout(new FlowLayout()); group = new CheckboxGroup(); checkbox1 = new Checkbox("Checkbox 1", group, false); checkbox1.addItemListener(this); add(checkbox1); checkbox2 = new Checkbox("Checkbox 2", group, false); checkbox2.addItemListener(this); add(checkbox2); checkbox3 = new Checkbox("Checkbox 3", group, false); checkbox3.addItemListener(this); add(checkbox3); setSize(300, 200); setVisible(true); } public void itemStateChanged(ItemEvent e) { Checkbox selectedCheckbox = group.getSelectedCheckbox(); if (selectedCheckbox != null) { System.out.println("Selected Checkbox: " + selectedCheckbox.getLabel()); } } public static void main(String[] args) { new CheckboxGroupExample(); } }
In this example, we create a CheckboxGroup
called group
and three Checkbox
components (checkbox1
, checkbox2
, and checkbox3
). We add each checkbox to the frame with add()
method.
We pass group
as the second argument to each Checkbox constructor, which groups the three Checkboxes together.
We also implement the ItemListener
interface and override the itemStateChanged()
method, which is called when a Checkbox’s state changes. In this method, we get the selected Checkbox from the CheckboxGroup using the getSelectedCheckbox()
method, and print its label to the console.
When we run this example and click on any of the Checkboxes, only one Checkbox in the group can be selected at a time, and the label of the selected Checkbox is printed to the console.
Java AWT CheckboxGroup Example with ItemListener:
Here’s an example of using CheckboxGroup in Java AWT with an ItemListener to create a simple user interface with grouped checkboxes:
import java.awt.*; import java.awt.event.*; public class CheckboxGroupExample implements ItemListener { CheckboxGroup group; Checkbox checkbox1, checkbox2, checkbox3; public CheckboxGroupExample() { Frame frame = new Frame("Checkbox Group Example"); frame.setLayout(new FlowLayout()); group = new CheckboxGroup(); checkbox1 = new Checkbox("Checkbox 1", group, false); checkbox1.addItemListener(this); frame.add(checkbox1); checkbox2 = new Checkbox("Checkbox 2", group, false); checkbox2.addItemListener(this); frame.add(checkbox2); checkbox3 = new Checkbox("Checkbox 3", group, false); checkbox3.addItemListener(this); frame.add(checkbox3); frame.setSize(300, 200); frame.setVisible(true); } public void itemStateChanged(ItemEvent e) { Checkbox selectedCheckbox = group.getSelectedCheckbox(); if (selectedCheckbox != null) { System.out.println("Selected Checkbox: " + selectedCheckbox.getLabel()); } } public static void main(String[] args) { new CheckboxGroupExample(); } }
In this example, we create a CheckboxGroup
called group
and three Checkbox
components (checkbox1
, checkbox2
, and checkbox3
). We add each checkbox to the frame with frame.add()
method.
We pass group
as the second argument to each Checkbox constructor, which groups the three Checkboxes together.
We also implement the ItemListener
interface and override the itemStateChanged()
method, which is called when a Checkbox’s state changes. In this method, we get the selected Checkbox from the CheckboxGroup using the getSelectedCheckbox()
method, and print its label to the console.
When we run this example and click on any of the Checkboxes, only one Checkbox in the group can be selected at a time, and the label of the selected Checkbox is printed to the console.