by Dinesh Thakur Category: AWT and Applets

The check box, as it is also known java.awt.Checkbox component is a component used to plot an option which can be connected (on ​​= true) or off (off = false). It is usually used to display a set of options which can be selected independently by the user, or allow multiple selectionsThese controls also have a label associated with them which describes the option of the checkbox. This control supports many constructors.

Checkbox chbox=new Checkbox();
Checkbox chbox=new Checkbox(String str);
Checkbox chbox=new Checkbox(String str, boolean true);

Given above are three types of constructor for the checkbox. The first form creates the default checkbox. The label for the check box can be set using the setLabel() method. The second constructor creates a checkbox with the specified label. The last one creates a checkbox that can be set as checked or unchecked.
The state of a checkbox may change at any instant of time. Therefore a method called
setState(boolean on) is used to change the state of the checkbox. The method getState() is used to get the state of the checkbox. This method retums a boolean value. Since item state changes, it generates the event called Item Event, which is handled by the Item Listener interface.

 A checkbox component is a labelled or unlabelled square box that contains a checkmark when it is selected and nothing otherwise. Checkboxes are mostly used when you need to choose whether you want an option or not. By convention, any number of checkboxes in a group can be selected. It can be created by instantiating the Checkbox class.

Checkbox object is essentially a bi-state switch that remains in the user-selected (or programmatically set) state. The Checkbox is strictly an on/off component and generally depends on other UI objects (typically labels) to indicate what the two states really mean.

Checkbox Useful Interface







A Checkbox with no label is somewhat unusual, but possible; the third variant automatically adds the checkbox to a preexisting Checkbox group

Checkbox(String label)



Checkbox(String label, CheckboxGroup group, boolean state)





Return the containing CheckboxGroup object, if any



What is the Checkbox label text?



What is the current state of the box?



Move this box to a containing Checkbox group and pass null to remove it from all groups

setLabel(String label)


Paint a new text string in the same Checkbox; this implies a layout manager relayout

setState(boolean state)


Set the current state of the box


import java.awt.*;

class CheckBoxExample extends Frame
        setLayout(new FlowLayout()); 
        Label lblHobbies = new Label("HOBBIES");
        Checkbox chkSports = new Checkbox("Sports");
        Checkbox chkMusic = new Checkbox("Music ",true);
        Checkbox chkReading = new Checkbox("Reading");
   class CheckboxJavaExample
         public static void main(String args[])
                CheckBoxExample frame = new CheckBoxExample();
                frame.setTitle("Checkbox in Java Example");


About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles