Radio button and Radio button group

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


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

Leave a Reply

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