Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Java AWT Radio Buttons Explained

1. Introduction

In Java AWT, one can create Radio Buttons just like check boxes. In order to create AWT radio buttons at run-time, we actually create check box instances and then group it. As a result of grouping, they act as Radio Buttons.

Radio Buttons are the special kind of check boxes grouped together. In a group, we can select only one radio button item at a time. AWT draws a check box in a square shape and check box in a group as circle one. To follow this example, one should know how to create a basic frame window. The article link below tells how to create frame window.

Link: Create Java AWT Frame Window (Hub Pages Network Articles)

2. Display Radio Buttons in Frame Window

We need Checkbox and CheckboxGroup classes from Java AWT to create radio buttons. The below code shows the required import statements for this example:

Next, we declare a Label, a Checkbox Group and three Checkboxes as the member of AWT Frame Window. Below is the declaration:

The AWT CheckboxGroup groups a set of check boxes together. In the below code we create a check box group instance to group the check boxes. Here, constructor does not take any argument.

When we create the check box, we specify the above created check box group as the third parameter to the constructor. The first parameter is the name for the check box and it will display as a label when the control is displayed. Second parameter specifies the check state of the Radio Button. The check boxes named as Air, Land and Water share same check box group, Travel_Method . Now if we display the check boxes in any container, they appear as Radio Buttons.

Now, we have all the controls ready in the application memory. We use add() method of Frame Window to equip the controls one by one to the Frame Window. Note, we add only the check box instances to the Window, not the group. AWT know all three check boxes belongs to same group as we linked it to the group in the construction time.

At this stage, if we render the controls, it will display like the one in the below screenshot.

AWT Radio Button Example
AWT Radio Button Example.

3. The ItemListener and The ItemEvent

We should register our radio buttons with an Item Listener so that we can say which one user clicked. First, we extend the Frame Window from ItemListener class and then link our check boxes to it.

The itemStateChanged event handler receives an ItemEvent when the user clicks any of the three radio buttons. Inside the handler function we can use getItemSelectable() method to know which one produced this event. Once we have the instance name, we can call any of the Check box methods. In our case, we called the getLabel()  method of the Checkbox class to tell the user which radio button is clicked by him or her.

Below is the complete code for this Example:

AwtRadioButtonExample.java

FrameWin.java

The below video shows running this example:

Source code: Download From Google Drive

Categories: AWT, Java

Tags: , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.