Java AWT Radio Buttons Applet GUI

Java AWT Radio Buttons Applet GUI

Radio buttons give a more user friendly environment when the user has to select only one option among many. There is no radio button class in java.awt package. Radio buttons are a group of checkboxes grouped as one unit. In the unit, if another radio button is selected, the earlier gets automatically deselected. Because radio button is a check box, all the methods illustrated in "Java AWT Checkbox" can be used here.

In the following program, three checkboxes are created and grouped as one unit. The selection of the user is displayed as a label.

Example on Java AWT Radio Buttons Applet GUI

HTML Program to run the above applet.
File Name: Study.html

The applet is run in both browser and appletviewer and outputs are shown.

Java AWT Radio Buttons Applet GUI

cbg = new CheckboxGroup();
mbaBox = new Checkbox("MBA", cbg, true);
btechBox = new Checkbox("B.Tech", cbg, false);
archBox = new Checkbox("B.Arch", cbg, false);

Three checkboxes mbaBox, btechBox and archBox are created. Here, we use CheckboxGroup object cbg to group the three check boxes. Observe the Checkbox constructor. The first parameter is the check box label, the second parameter is the CheckboxGroup object and the third one is the boolean value that indicates by default selected are not. Now under cbg group three check boxes are grouped. Like this one application can have any number of groups grouped with different CheckboxGroup objects.

All the three check boxes are registered with ItemListener for event handling with addItemListener() method. itemStateChanged() method of ItemListener is overridden with required event handling code.

String str = "";
if(mbaBox.getState( ) == true)
str = "You study MBA.";

The getState() method returns the state of the check box; if selected returns true else false. As per the radio button selection, the temporary string variable str is assigned with a value.


The str value is set to the label lab.

3 thoughts on “Java AWT Radio Buttons Applet GUI

  1. v.s.

    sir y the same code in frame not working its not displaying neither checkboxes nor any radio button just showing the label “I Display Your Selection” at the middle of the frame

    import java.awt.*;
    import java.awt.event.*;
    public class Courses extends Frame implements ItemListener
    Checkbox mbaBox, btechBox, archBox;
    CheckboxGroup cbg;
    Label lab;
    public Courses()
    cbg = new CheckboxGroup();
    mbaBox = new Checkbox(“MBA”, cbg, true);
    btechBox = new Checkbox(“B.Tech”, cbg, true);
    archBox = new Checkbox(“B.Arch”, cbg, true);
    lab = new Label(“I Display Your Selection”);



    setTitle(“radio Boxes in Action”);
    setSize(300, 300);

    public void itemStateChanged(ItemEvent e)
    String str = “”;
    if(mbaBox.getState() == true)
    str = “You study MBA.”;
    else if(btechBox.getState() == true)
    str = “You study B.Tech.”;
    else if(archBox.getState() == true)
    str = “You study B.Arch.”;


    public static void main(String…aa)
    new Courses();


Leave a Reply

Your email address will not be published. Required fields are marked *