Way2Java

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
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Courses extends Applet implements ItemListener
{
  Checkbox mbaBox, btechBox, archBox;
  CheckboxGroup cbg;
  Label lab;
  public void init()
  {
    cbg = new CheckboxGroup();
    mbaBox = new Checkbox("MBA", cbg, true);
    btechBox = new Checkbox("B.Tech", cbg, false);
    archBox = new Checkbox("B.Arch", cbg, false);
    lab = new Label("I Display Your Selection");
            
    mbaBox.addItemListener(this);
    btechBox.addItemListener(this);
    archBox.addItemListener(this);

    add(mbaBox);
    add(btechBox);
    add(archBox);
    add(lab);
  }
  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.";
           
    lab.setText(str);
  }
 }

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


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

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.

lab.setText(str);

The str value is set to the label lab.

Application to Applet Migration

Following steps can be noticed in the above program to develop an applet instead of application.

  1. Import java.applet package
  2. Extend Applet instead of Frame
  3. Write init() method in place of constructor
  4. No main() method
  5. Write an extra HTML file to run the applet
  6. No set methods like setSize() as they are given in HTML file
  7. No window closing code as window closing is implicit

Remember the default layout manager for Applet is FlowLayout which can be changed (like BorderLayout for Frame). In the above program, the default FlowLayout is used.

Note: In applet, window closing code is not required. It is automatic.

Note: User's selection, messages or component information etc. can be displayed in five ways. In this program, it is displayed as a Label component.