Radio button and Radio button group


1. What is Radio button?

Infact, there is no radio button class in Java. Radio buttons are nothing but checkboxes only but grouped as one unit.

2. What the advantage of grouping checkboxes?

Once grouped, in the group if one checkbox is selected, the other gets automatically deselected. This is the only advantage. Sometimes, it is required to make user-friedly code. For example,

Think there are three checkboxes of Hero Honda, Pulsar and Activa in which user has to select ONLY ONE vehicle. User first selected Pulsar then changed his mind to Hero Honda and selected Hero Honda. Now two checkboxes are selected of Pulsar and Hero Honda. User is supposed to select only one vehicle. Then, which vehicle the dealer should supply. The mistake of the user is he has not deselected the earlier Pulsar when he opted for Hero Honda. This mistake of the user can be corrected by the Programmer with radio buttons. With radio buttons, if Hero Honda is selected, the earlier Pulsar is automatically gets deselected.

3. How to group checkboxes?

To group checkboxes as one unit, there comes a Java class CheckboxGroup. A program can contained any number of checkbox groups like for vehicles, for colors and for instalment schemes etc.

4. How to know which radio button user selected?

There is no radio button as such. Radio button is a checkbox. Use all the methods of checkbox for radio button also. The Checkbox selection is known to Programmer with getSelected() method. This method returns true if the checkbox (here, radio button) selected by the user, else false.

After learning about Radio button and Radio button group let us go for the event handling.

5. What is the event generated by radio buttons and what listener is used?

I repeat many times the same word. Radio button is a checkbox. So, radio button (checkbox) generates ItemEvent and handled by ItemListener.

Following gives a simple example (later we see a big program) of radio buttons where one group only is created to get the concept. Event handling is also done. User selection is displayed at DOS prompt for simplicity of code.Radio button and Radio button group

Example on Radio button and Radio button group

import java.awt.*;
import java.awt.event.*;
public class RadioExample extends Frame implements ItemListener
{
  Checkbox hondaButton, pulsarButton, activaButton;
  public RadioExample()
  {				            // first set the layout to frame
    setLayout(new FlowLayout());
				            // create one radio button group
    CheckboxGroup cbg1 = new CheckboxGroup();
				            // create radio buttons and add to group cbg1
    hondaButton = new Checkbox("Hero Honda", cbg1, true); // true means default selected
    pulsarButton = new Checkbox("Pulsar", cbg1, false);   // false means  default not selected
    activaButton = new Checkbox("Activa", cbg1, false);   // false means default not selected);
					    // link radio buttons to ItemListener
    hondaButton.addItemListener(this);
    pulsarButton.addItemListener(this);
    activaButton.addItemListener(this);
				            // add radio buttons to frame
    add(hondaButton);   add(pulsarButton);  add(activaButton);
					    // then frame set methods
    setTitle("Simple Radio Button Example");
    setSize(300, 200);
    setVisible(true);
  }
                                            // override the only abstract method of ItemListener interface
  public void itemStateChanged(ItemEvent e)
  {
      boolean b1 = hondaButton.getState();
      boolean b2 = pulsarButton.getState();
      boolean b3 = activaButton.getState();

      System.out.println("\nYou selected Hero Honda: " + b1);
      System.out.println("You selected Pulsar: " + b2);
      System.out.println("You selected Activa: " + b3);
  }
  public static void main(String args[])
  {
    new RadioExample();
  }
}


Radio button and Radio button group
Output screen of Radio button and Radio button group when multiple options are selected.

Leave a Comment

Your email address will not be published.