Home » Java AWT CheckBox

Java AWT CheckBox

by Online Tutorials Library

Java AWT Checkbox

The Checkbox class is used to create a checkbox. It is used to turn an option on (true) or off (false). Clicking on a Checkbox changes its state from “on” to “off” or from “off” to “on”.

AWT Checkbox Class Declaration

Checkbox Class Constructors

Sr. no. Constructor Description
1. Checkbox() It constructs a checkbox with no string as the label.
2. Checkbox(String label) It constructs a checkbox with the given label.
3. Checkbox(String label, boolean state) It constructs a checkbox with the given label and sets the given state.
4. Checkbox(String label, boolean state, CheckboxGroup group) It constructs a checkbox with the given label, set the given state in the specified checkbox group.
5. Checkbox(String label, CheckboxGroup group, boolean state) It constructs a checkbox with the given label, in the given checkbox group and set to the specified state.

Method inherited by Checkbox

The methods of Checkbox class are inherited by following classes:

  • java.awt.Component
  • java.lang.Object

Checkbox Class Methods

Sr. no. Method name Description
1. void addItemListener(ItemListener IL) It adds the given item listener to get the item events from the checkbox.
2. AccessibleContext getAccessibleContext() It fetches the accessible context of checkbox.
3. void addNotify() It creates the peer of checkbox.
4. CheckboxGroup getCheckboxGroup() It determines the group of checkbox.
5. ItemListener[] getItemListeners() It returns an array of the item listeners registered on checkbox.
6. String getLabel() It fetched the label of checkbox.
7. T[] getListeners(Class listenerType) It returns an array of all the objects registered as FooListeners.
8. Object[] getSelectedObjects() It returns an array (size 1) containing checkbox label and returns null if checkbox is not selected.
9. boolean getState() It returns true if the checkbox is on, else returns off.
10. protected String paramString() It returns a string representing the state of checkbox.
11. protected void processEvent(AWTEvent e) It processes the event on checkbox.
12. protected void processItemEvent(ItemEvent e) It process the item events occurring in the checkbox by dispatching them to registered ItemListener object.
13. void removeItemListener(ItemListener l) It removes the specified item listener so that the item listener doesn’t receive item events from the checkbox anymore.
14. void setCheckboxGroup(CheckboxGroup g) It sets the checkbox’s group to the given checkbox.
15. void setLabel(String label) It sets the checkbox’s label to the string argument.
16. void setState(boolean state) It sets the state of checkbox to the specified state.

Java AWT Checkbox Example

In the following example we are creating two checkboxes using the Checkbox(String label) constructo and adding them into the Frame using add() method.

CheckboxExample1.java

Output:

java awt checkbox example 1

Java AWT Checkbox Example with ItemListener

In the following example, we have created two checkboxes and adding them into the Frame. Here, we are adding the ItemListener with the checkbox which displays the state of the checkbox (whether it is checked or unchecked) using the getStateChange() method.

CheckboxExample2.java

Output:

java awt checkbox example 2


You may also like