Java Tutorial

AWT Radio Buttons

In java.awt we are not having a special class for radio buttons but we can create radio button from Checkbox class.

java.awt we have a predefined class called CheckboxGroup through which we can add all the checkboxes to the object of CheckboxGroup class.

Steps for converting checkboxes into radiobutton:

1. Create objects of CheckboxGroup class.

For example:

CheckboxGroup cbg1=new CheckboxGroup (); 
CheckboxGroup cbg2=new CheckboxGroup (); 
CheckboxGroup cbg3=new CheckboxGroup (); 
CheckboxGroup cbg4=new CheckboxGroup ();

CheckboxGroup object allows to select a single checkbox among 'n' number of check boxes.

2. Create 'n' number of objects of Checkbox class.

For example:

Checkbox b1, b2, b3, b4;
b1=new Checkbox ("C"); 
b2=new Checkbox ("Cpp");
b3=new Checkbox ("Java");
b4=new Checkbox ("Oracle9i");

3. Decide which checkboxes are adding to which CheckboxGroup object. In order to add the checkboxes to the CheckboxGroup object, in Check box class we have the following method:

public void setCheckboxGroup (CheckboxGroup);

For example:

cb1.setCheckboxGroup (cbg1); 
cb2.setCheckboxGroup (cbg2); 
cb3.setCheckboxGroup (cbg3); 
cb4.setCheckboxGroup (cbg4);

4. Register the events of checkbox with ItemListener by using the following method:

public void addItemListener (ItemListener);

Note: Instead of using a setCheckboxGroup method we can also used the following Constructor which is present in Check box class.

Checkbox (String, CheckboxGroup, boolean);
Checkbox (String, boolean, CheckboxGroup);

For example:

Checkbox cb1=new Checkbox ("C", cbg1, false);
Checkbox cb2=new Checkbox ("Cpp", cbg2, false); 
Checkbox cb3=new Checkbox ("Java", cbg3, false); 
Checkbox cb4=new Checkbox ("Oracle9i", cbg4, false);

Write a java program which illustrates the concept of Radio Button?

Answer:

import java.awt.*; 
import java.awt.event.*;
class RadioApp extends Frame
{
    Checkbox cb1, cb2, cb3, cb4, cb5; 
    Label l, l1;
    RadioApp ()
    {
        setSize (200, 200);
        setLayout (new FlowLayout ()); 
        CheckboxGroup cbg1=new CheckboxGroup(); 
        CheckboxGroup cbg2=new CheckboxGroup(); 
        cb1=new Checkbox ("C", cbg1, false); 
        cb2=new Checkbox ("Cpp", cbg1, false); 
        cb3=new Checkbox ("Java", cbg2, false); 
        cb4=new Checkbox ("Oracle9i", cbg2, false); 
        cb5=new Checkbox ("exit");
        l=new Label ("Course : "); 
        l1=new Label ();
        add (l);
        add (cb1);
        add (cb2);
        add (cb3);
        add (cb4);
        add (cb5);
        add (l1);
        cb1.addItemListener (new itl ()); 
        cb2.addItemListener (new itl ()); 
        cb3.addItemListener (new itl ()); 
        cb4.addItemListener (new itl ()); 
        cb5.addItemListener (new itl ()); 
        setVisible (true);
    }
    class itl implements ItemListener
    {
        public void itemStateChanged (ItemEvent ie)
        {
            Object obj=ie.getItemSelectable (); 
            Checkbox cb=(Checkbox) obj;
            if (cb.getState ())
            {
                l1.setText ("U HAVE SELECTED : "+cb.getLabel ()); 
                String lab=cb.getLabel ();
                if (lab.equalsIgnoreCase ("exit"))
                {
                     System.exit (0);

                }
            }
            else
            {
                l1.setText ("U HAVE SELECTED NONE");
            }
        }
    }
};
    
class RadioAppDemo
{
    public static void main (String [] args)
    {
        new RadioApp ();
    }
};