In this article Radio Button in AWT we give the information about Radio Button is used when we need to select only one option from a group. It works as a round button to select options, where only one button can be active.
Radio Button in AWT:
There is no component directly known as Radio Button in AWT (Abstract Window Toolkit), but Radio Button functionality can be created using Checkbox. For this we use CheckboxGroup, from which only one option can be selected from a group. When we place a Checkbox in a CheckboxGroup, it behaves like a Radio Button.
Function of Radio Button:
Radio Button is used when we need to select only one option from a group.
It works as a round button to select options, where only one button can be active.
Using CheckboxGroup to create a Radio Button:
How to create Radio Button in AWT: In AWT we can create a component that behaves like a Radio Button by using Checkbox. For this we use CheckboxGroup, so that only one Checkbox from a group can be selected.
Main features of Radio Button
CheckboxGroup
Description: CheckboxGroup is used to select only one option from a group. Only one Radio Button (Checkbox) can be selected at a time.
Method:
getCheckboxGroup(): Gets the current group of Checkboxes.
setCheckboxGroup(CheckboxGroup group): Adds the Checkbox to a group.
Example:
CheckboxGroup group = new CheckboxGroup();
Checkbox radio1 = new Checkbox(“Male”, group, false);
Checkbox radio2 = new Checkbox(“Female”, group, true);
Label
Description: This is the text that appears next to the Radio Button. It explains what that option is.
Method:
getLabel(): Gets the label of the Checkbox.
setLabel(String label): Sets the label of the Checkbox.
Example:
Checkbox radio1 = new Checkbox(“Male”, group, false);
radio1.setLabel(“Male”);
State
Description: Returns whether the Radio Button is currently selected (checked). If Radio Button is selected then its status will be true, otherwise false.
Method:
getState(): Gets the current state of the Checkbox (checked or unchecked).
setState(boolean state): Sets the state of the Checkbox (checked or unchecked).
Example:
Checkbox radio1 = new Checkbox(“Male”, group, false);
radio1.setState(true); // Checking Radio Button
Enabled/Disabled
Description: This property tells whether the Radio Button is enabled or disabled. If it is inactive it cannot be selected.
Method:
setEnabled(boolean status): Enables or disables the Checkbox.
isEnabled(): Checks whether the Checkbox is active or not.
Example:
radio1.setEnabled(false); //Disabling the Radio Button
Foreground:
Description: Sets the color of the label (text) of the Radio Button.
Method:
setForeground(Color c): Sets the text color of the Checkbox (Radio Button).
Example:
radio1.setForeground(Color.RED); // turning the radio button text color red
Background
Description: Sets the background color of the Radio Button.
Method:
setBackground(Color c): Sets the background color of the Checkbox (Radio Button).
Example:
radio1.setBackground(Color.YELLOW); // make background color yellow
Font
Description: Sets the font of the Radio Button’s label, such as font style, size, etc.
Method:
setFont(Font f): Sets the font of the text of the Checkbox (Radio Button).
Example:
Font font = new Font(“Arial”, Font.BOLD, 14);
radio1.setFont(font); // Setting the font of the Radio Button’s label
ItemListener
Description: Handles the ItemListener event when the Radio Button’s selection (checked) changes. This allows you to react to selection or de-selection.
Method:
addItemListener(ItemListener l): Adds an item listener to the checkbox, which reacts when it is checked or unchecked.
Example:
radio1.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
System.out.println(“Radio Button selected”);
}
});
Bounds
Description: This property sets the position (x, y) and size (width, height) of the Radio Button. This decides where and at what size the Radio Button will be displayed on the screen.
Method:
setBounds(int x, int y, int width, int height): Sets the position and size of the Radio Button.
Example:
radio1.setBounds(100, 100, 150, 50); // Setting the position and size of the Radio Button
//Program Code (Example of Radio Button in AWT):
import java.awt.*;
import java.awt.event.*;
public class RadioButtonExample {
public static void main(String[] args) {
// Create Frame
final Frame f = new Frame(“AWT RadioButton Example”);
// Create CheckboxGroup
CheckboxGroup group = new CheckboxGroup();
// Create Radio Buttons (Checkboxes) and join to CheckboxGroup
Checkbox radio1 = new Checkbox(“Male”, group, false);
radio1.setBounds(100, 100, 100, 50);
radio1.setForeground(Color.RED);
radio1.setBackground(Color.YELLOW);
Font font1 = new Font(“Arial”, Font.BOLD, 14);
radio1.setFont(font1); // Setting the font of the Radio Button’s label
Checkbox radio2 = new Checkbox(“Female”, group, false);
radio2.setBounds(100, 150, 100, 50);
radio2.setForeground(Color.RED);
radio2.setBackground(Color.YELLOW);
Font font2 = new Font(“Arial”, Font.BOLD, 14);
radio2.setFont(font2); // Setting the font of the Radio Button’s label
// Join ItemListener to Radio Button
radio1.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
System.out.println(“Male Radio Button Selected”);
}
});
radio2.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
System.out.println(“Female Radio Button Selected”);
}
});
// Join Radio Buttons to Frame
f.add(radio1);
f.add(radio2);
// Setting in Frame
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
// Close the Frame using WindowListener
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
f.dispose();
} });
} }
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining