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:
1 2 3 4 5 |
//Sample 07: Required Imports for this Example import java.awt.Checkbox; import java.awt.CheckboxGroup; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; |
Next, we declare a Label, a Checkbox Group and three Checkboxes as the member of AWT Frame Window. Below is the declaration:
1 2 3 4 5 |
//Sample 08: Declare Checkboxes //and CheckboxGroup as members Checkbox cbAir, cbLand, cbWater; CheckboxGroup TravelMethod; Label label; |
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.
1 2 |
//Sample 09: Create CheckboxGroup CheckboxGroup Travel_Method = new CheckboxGroup(); |
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.
1 2 3 4 5 |
//Sample 10: Create a Group to Hold Radio buttons cbAir = new Checkbox("Air", false, Travel_Method ); cbLand = new Checkbox("Land", false, Travel_Method); cbWater = new Checkbox("Water", true, Travel_Method); label = new Label(); |
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.
1 2 3 4 5 6 |
//Sample 11: Add Checkboxes and Label //to FrameWindow add(cbAir); add(cbLand); add(cbWater); add(label); |
At this stage, if we render the controls, it will display like the one in the below screenshot.

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.
1 2 3 4 |
//Sample 12: Add Listener for Checkbox cbAir.addItemListener(this); cbLand.addItemListener(this); cbWater.addItemListener(this); |
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
1 2 3 4 5 6 7 |
public class AwtRadioButtonExample { public static void main(String[] args) { //Sample 06: Create Frame and Display it FrameWin fw = new FrameWin("CheckBox Example"); fw.setVisible(true); } } |
FrameWin.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Label; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; //Sample 07: Required Imports for this Example import java.awt.Checkbox; import java.awt.CheckboxGroup; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; public class FrameWin extends Frame implements WindowListener, ItemListener { //Sample 08: Declare Checkboxes as members Checkbox cbAir, cbLand, cbWater; CheckboxGroup TravelMethod; Label label; //Sample 01: Constructor public FrameWin(String FrameTitle){ //Sample 02: Set Layout and Title super(FrameTitle); setLayout(new FlowLayout()); //Sample 09: Create CheckboxGroup CheckboxGroup Travel_Method = new CheckboxGroup(); //Sample 10: Create a Group to Hold Radio buttons cbAir = new Checkbox("Air", false, Travel_Method ); cbLand = new Checkbox("Land", false, Travel_Method); cbWater = new Checkbox("Water", true, Travel_Method); label = new Label(); //Sample 11: Add Checkboxes and Label //to FrameWindow add(cbAir); add(cbLand); add(cbWater); add(label); //Sample 12: Add Listener for Checkbox cbAir.addItemListener(this); cbLand.addItemListener(this); cbWater.addItemListener(this); //Sample 03: Set Size of the Frame setSize(400, 100); setLocation(100,100); //Sample 04: Register with the Listener addWindowListener(this); } //Sample 05: Implement the Listeners public void windowOpened(WindowEvent e) {} public void windowClosing(WindowEvent e) { this.dispose(); } public void windowClosed(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} //Sample 13: Show the Selected Radio Button public void itemStateChanged(ItemEvent e) { Checkbox Selected = (Checkbox) e.getItemSelectable(); label.setText("Selected Travel Method is: " + Selected.getLabel()); } } |
The below video shows running this example:
Source code: Download From Google Drive
Tags: AWT, CheckboxGroup, ItemEvent, ItemListener