Java AWT (Abstract Window Toolkit) provides a really various set of tools for edifice in writing user interfaces (GUIs), and among these tools is the Checkbox class. Checkboxes are necessary components for user interactions, allowing users to work binary choices easily. In this clause, we'll search the Checkbox class, its constructors, and methods, and supply examples of creating checkboxes with really different logic.
Java AWT Checkbox Class Declaration
The Checkbox class in Java AWT is secondhand to create checkboxes, which represent options that users can either select or deselect. Clicking on a checkbox toggles its submit between "on" (selected) and "off" (deselected).
The sort-out is explicit as follows:
public class Checkbox extends Component implements ItemSelectable, Accessible
Constructors of Checkbox class
|
Creates a checkbox with no label.
|
Creates a checkbox with the specified label.
|
Creates a checkbox with the given label and sets the initial state.
|
Creates a checkbox with the specified label, state, and associates it with a CheckboxGroup.
|
Creates a checkbox with the given label, associates it with the specified CheckboxGroup, and sets the initial state.
|
Methods of checkbox class are inherited by two classes:
- java.awt.Component
- java.lang.Object
Key Methods of Checkbox class
|
Adds the specified item listener to receive events when the state of the checkbox changes.
|
Retrieves the accessible context for enhanced interaction with assistive technologies for the checkbox.
|
Creates the peer of the checkbox for rendering purposes.
|
Retrieves the associated CheckboxGroup to which the checkbox belongs.
|
Returns an array of registered item listeners responsible for handling item events of the checkbox.
|
Retrieves the label associated with the checkbox.
|
Returns an array containing the label of the checkbox if it is selected; otherwise, returns null.
|
Returns true if the checkbox is in the selected state; otherwise, returns false.
|
Generates a string representation of the state and properties of the checkbox.
|
Processes the general events occurring on the checkbox.
|
Processes the item events occurring in the checkbox by dispatching them to registered ItemListener objects.
|
Removes the specified item listener to stop receiving item events from the checkbox.
|
Sets the checkbox's group to the specified CheckboxGroup.
|
Sets the label of the checkbox to the specified string argument.
|
Sets the state of the checkbox to the specified state, either selected (true) or deselected (false).
|
Example of Java AWT Checkbox
Example 1:
Java
// Java Program to demonstrate
// Creating a Java AWT Checkbox
import java.awt.*;
public class one {
//Constructor to initialize
public one() {
// Creating a Frame
Frame frame = new Frame("Simple Checkbox Example");
// Creating a Checkbox with the label "Agree to terms and conditions"
Checkbox checkbox = new Checkbox("Agree to geeksforgeeks terms and conditions");
// Setting Checkbox position
checkbox.setBounds(50, 50, 250, 30);
// Adding Checkbox to the frame
frame.add(checkbox);
// Setting Frame size
frame.setSize(400, 200);
// Setting Layout to null
frame.setLayout(null);
// Making the frame visible
frame.setVisible(true);
}
public static void main(String[] args) {
new one();
}
}
Output:

Example 2:
Java
// Java Program to demonstrate Checkbox example using an
// ItemListener
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class one {
// Constructor to initialize
public one()
{
// Creating a Frame
Frame frame = new Frame(
"Checkbox with ItemListener Example");
// Creating a Checkbox
Checkbox checkbox = new Checkbox("Enable Feature");
// Setting Checkbox position
checkbox.setBounds(50, 50, 150, 30);
// Adding ItemListener to Checkbox
checkbox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e)
{
// Handling Checkbox state change event
if (checkbox.getState()) {
System.out.println(
"Feature is enabled");
}
else {
System.out.println(
"Feature is disabled");
}
}
});
// Adding Checkbox to the frame
frame.add(checkbox);
// Setting Frame size
frame.setSize(400, 200);
// Setting Layout to null
frame.setLayout(null);
// Making the frame visible
frame.setVisible(true);
}
public static void main(String[] args) { new one(); }
}
Output

When check box gets unchececked

Similar Reads
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
3 min read
JavaFX | Checkbox CheckBox is a part of JavaFX package. CheckBox is a box with a tick on it when selected and empty when not selected. At first, checkboxes may seem similar to radio buttons, but there exists the difference between them that checkboxes cannot be combined into toggle groups, which means we cannot selec
4 min read
JavaFX | ChoiceBox ChoiceBox is a part of the JavaFX package. ChoiceBox shows a set of items and allows the user to select a single choice and it will show the currently selected item on the top. ChoiceBox by default has no selected item unless otherwise selected. One may either specify the items and then the selected
4 min read
Bulma Checkbox In this article, we'll be learning about Bulma Checkbox, with some of its examples. Bulma is a free and open-source framework used to build reusable components while building web applications. This framework is a mobile-ready framework with which the users can see the web applications as like a mobi
2 min read
CheckBox in Android CheckBox is used for adding multiple selections of items from the given set of options. This is seen used in many android applications for adding a feature for multiple selections. In this article, we will take a look at How to implement Checkbox in Android. A sample video is given below to get an i
4 min read