JAVA: Creating Checkboxes & RadioButtons

Checkboxes are used for multi option user input that the user may select or de-select by clicking on them. A RadioButtonis similar to a Checkbox; they are basically used as option buttons to specify choices. The difference between radiobuttons and checkboxes is that only a single radiobutton can be selected in a RadioButton group, as opposed to checkboxes where more than one CheckBox can be selected.

To create checkboxes or radiobuttons, its initial state has to be set, that is, whether selected or unselected.

Certain steps are to be performed for creating checkboxes or radiobuttons. They are:

1.      Create the element (checkbox or radiobutton)

2.      Decide its initial state

3.      Decide where to place it on the screen

4.      Display it on the screen

The CheckBox component can also create radio buttons by using an additional class called CheckboxGroup.

Some of the constructors for creating checkboxes in Java are:

  • Checkbox( ): Creates a check box whose label is not specified initially. The state of the checkbox is unchecked.
  • Checkbox(String text): Creates a checkbox with given string as label. The state of the checkbox is unchecked.
  • Checkbox(String text, boolean on): Creates a checkbox with a given string as label and allows to set the state of the checkbox by setting the value of the boolean variable as true or false.

To create radio buttons, first a CheckboxGroup object is created as shown in the following code:

CheckboxGroup cg =  new CheckboxGroup( );

Then each of the radio buttons is created as shown in the following code:

Checkbox male = new Checkbox("male", cg, true);
Checkbox female = new Checkbox("female", cg, false);

The first argument is the string which will appear as label, the second argument specifies the group to which the checkbox belongs and the third argument specifies whether it will be selected or not. If this checkbox doesn’t belong to a group then null must be specified in the second argument.

The setState( ) and getState( ) methods are used to set and retrieve the state of a checkbox. Syntax of these methods is:

  • boolean getState()
  • void setState(boolean on)

The following code demonstrates the use of checkboxes:

import java.awt.*;
import java.awt.event.*;
class Hobbies extends Frame
{
    Checkbox cboxRead = new Checkbox("Reading", false);
    Checkbox cboxMus = new Checkbox("Music", false);
    Checkbox cboxPaint = new Checkbox("Painting", false);
    Checkbox cboxMovie = new Checkbox("Movie", false);
    Checkbox cboxDance = new Checkbox("Dancing", false);
    Label lblQts = new Label("What's your hobby?");
    public Hobbies(String str)
    {
        super(str);
        setLayout(new GridLayout(6,1));
        add(lblQts);
        add(cboxRead);
        add(cboxMus);
        add(cboxPaint);
        add(cboxMovie);
        add(cboxDance);
        addWindowListener (new WindowAdapter()
        {
            public void windowClosing(WindowEvent we)
            {
                setVisible(false);
                System.exit(0);
            }
        });
    }
    public static void main(String args[])
    {
        Hobbies ObjHobby = new Hobbies("A basket full of checkboxes");
        ObjHobby .setSize(300, 300);
        // ObjHobby.pack();
        ObjHobby.show();
    }
}

which is unchecked, false is specified as a parameter to the Checkbox constructor along with the String parameter for the label of the checkbox. The pack() method can also be used instead of setSize(). In that case the components would have been neatly packed in the container. This is required when the size of the container needed to include all the components is not known.

The output of the above code is:

Now let’s discuss the creation of radio buttons:

import java.awt.*;
import java.awt.event.*;
class Qualification extends Frame
{
    CheckboxGroup cg = new CheckboxGroup();
    Checkbox radUnder = new Checkbox("Undergraduate", cg, false);
    Checkbox radGra = new Checkbox("Graduate", cg, false);
    Checkbox radPost = new Checkbox("Post Graduate", cg, false);
    Checkbox radDoc = new Checkbox("Doctorate", cg, false);
    Label lblQts = new Label("What's your Qualification?");
    public Qualification(String str)
    {
        super(str);
        setLayout(new GridLayout(6,1));
        add(lblQts);
        add(radUnder);
        add(radGra);
        add(radPost);
        add(radDoc);
        addWindowListener (new WindowAdapter()
        {
            public void windowClosing(WindowEvent we)
            {
                setVisible(false);
                System.exit(0);
            }
        });
    }
    public static void main(String args[])
    {
        Qualification ObjQualification = new Qualification("Literacy!!");
        ObjQualification.pack();
        ObjQualification.show();
    }
}

The CheckboxGroup class can be used to create radiobuttons. In this example, three parameters are passed to the constructor; the String label, the CheckboxGroup instance and the initial state of the radiobutton.

The code generates the following output: