by Dinesh Thakur

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

 

Method

Returns

Notes

Checkbox()

 

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)

 

 

getCheckboxGroup()

CheckboxGroup

Return the containing CheckboxGroup object, if any

getLabel()

String

What is the Checkbox label text?

getState()

Boolean

What is the current state of the box?

setCheckboxGroup(CheckboxGroup)

Void

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

setLabel(String label)

Void

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

setState(boolean state)

Void

Set the current state of the box

 

import java.awt.*;

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

Checkbox