JRadioButton is a Swing component in Java used to create radio buttons, allowing users to select a single option from a group of mutually exclusive options.
Radio buttons are typically grouped together using a ButtonGroup, which ensures that only one radio button can be selected at a time within the group.
In this tutorial, we will explore:
Table of Contents
1. Creating a Basic JRadioButton
A JRadioButton can be created by using the constructor and specifying the label text. Radio buttons can be placed individually, but are usually part of a group of buttons where only one can be selected.
Example 1: Basic JRadioButton
import javax.swing.*; public class JRadioButtonExample1 { public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame("Basic JRadioButton Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200); // Create individual radio buttons JRadioButton radioButton1 = new JRadioButton("Option 1"); JRadioButton radioButton2 = new JRadioButton("Option 2"); frame.setLayout(new java.awt.FlowLayout()); frame.add(radioButton1); frame.add(radioButton2); frame.setVisible(true); }); } }
Explanation:
Two radio buttons (radioButton1 and radioButton2) are created and added to the frame.
Each radio button is independent in this example, so both can be selected at the same time.
2. Grouping Radio Buttons Using ButtonGroup
To ensure that only one radio button can be selected at a time, you must group the radio buttons using the ButtonGroup class.
Example 2: Grouping JRadioButtons with ButtonGroup
import javax.swing.*; public class JRadioButtonExample2 { public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame("ButtonGroup Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200); // Create radio buttons JRadioButton radioButton1 = new JRadioButton("Option 1"); JRadioButton radioButton2 = new JRadioButton("Option 2"); JRadioButton radioButton3 = new JRadioButton("Option 3"); // Group the radio buttons ButtonGroup group = new ButtonGroup(); group.add(radioButton1); group.add(radioButton2); group.add(radioButton3); frame.setLayout(new java.awt.FlowLayout()); frame.add(radioButton1); frame.add(radioButton2); frame.add(radioButton3); frame.setVisible(true); }); } }
Explanation:
A ButtonGroup is created to group three radio buttons.
Only one of the grouped radio buttons can be selected at any time. Selecting another radio button automatically deselects the previous one.
3. Handling Radio Button Selection Events
You can handle selection events by adding an ActionListener to each JRadioButton. This allows you to perform actions based on which button is selected.
Example 3: Handling Radio Button Selection Events
import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class JRadioButtonExample3 { public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame("Radio Button Selection Event Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200); // Create radio buttons JRadioButton radioButton1 = new JRadioButton("Option 1"); JRadioButton radioButton2 = new JRadioButton("Option 2"); // Group the radio buttons ButtonGroup group = new ButtonGroup(); group.add(radioButton1); group.add(radioButton2); JLabel label = new JLabel("No selection"); // Add action listeners to handle selection radioButton1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { label.setText("Selected: Option 1"); } }); radioButton2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { label.setText("Selected: Option 2"); } }); frame.setLayout(new java.awt.FlowLayout()); frame.add(radioButton1); frame.add(radioButton2); frame.add(label); frame.setVisible(true); }); } }
Explanation:
Action listeners are added to each radio button to handle selection changes.
When a radio button is selected, the corresponding action is performed, updating a label to show the selected option.
4. Pre-selecting a Radio Button
You can pre-select one of the radio buttons by calling the setSelected(true) method. This is useful when you want a default option to be selected when the form loads.
Example 4: Pre-selecting a Radio Button
import javax.swing.*; public class JRadioButtonExample4 { public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame("Pre-selected Radio Button Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200); // Create radio buttons JRadioButton radioButton1 = new JRadioButton("Option 1"); JRadioButton radioButton2 = new JRadioButton("Option 2"); // Pre-select Option 2 radioButton2.setSelected(true); // Group the radio buttons ButtonGroup group = new ButtonGroup(); group.add(radioButton1); group.add(radioButton2); frame.setLayout(new java.awt.FlowLayout()); frame.add(radioButton1); frame.add(radioButton2); frame.setVisible(true); }); } }
Explanation:
setSelected(true) is used to pre-select radioButton2 (Option 2). When the application starts, Option 2 will be selected by default.
5. Custom Action Listeners for Radio Buttons
You can use custom action listeners with radio buttons to trigger specific behaviors when they are selected.
Example 5: Custom Action Listeners for JRadioButton
import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class JRadioButtonExample5 { public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame("Custom ActionListener Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200); // Create radio buttons JRadioButton radioButton1 = new JRadioButton("Option 1"); JRadioButton radioButton2 = new JRadioButton("Option 2"); // Group the radio buttons ButtonGroup group = new ButtonGroup(); group.add(radioButton1); group.add(radioButton2); // Define a custom action listener class class RadioButtonActionListener implements ActionListener { private String message; public RadioButtonActionListener(String message) { this.message = message; } @Override public void actionPerformed(ActionEvent e) { System.out.println(message); } } // Attach custom listeners to the radio buttons radioButton1.addActionListener(new RadioButtonActionListener("Option 1 Selected")); radioButton2.addActionListener(new RadioButtonActionListener("Option 2 Selected")); frame.setLayout(new java.awt.FlowLayout()); frame.add(radioButton1); frame.add(radioButton2); frame.setVisible(true); }); } }
Explanation:
A custom ActionListener class (RadioButtonActionListener) is created to handle radio button selection events.
The listener prints a message to the console based on which button is selected.
6. Customizing the Appearance of Radio Buttons
You can customize the appearance of JRadioButton by modifying its font, text color, and background color to match the design of your application.
Example 6: Customizing the Appearance of JRadioButton
import javax.swing.*; import java.awt.*; public class JRadioButtonExample6 { public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame("Customized JRadioButton Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200); // Create radio buttons JRadioButton radioButton1 = new JRadioButton("Option 1"); JRadioButton radioButton2 = new JRadioButton("Option 2"); // Customize the appearance radioButton1.setFont(new Font("Serif", Font.BOLD, 16)); radioButton1.setForeground(Color.BLUE); radioButton1.setBackground(Color.LIGHT_GRAY); radioButton2.setFont(new Font("Serif", Font.BOLD, 16)); radioButton2.setForeground(Color.RED); radioButton2.setBackground(Color.LIGHT_GRAY); // Group the radio buttons ButtonGroup group = new ButtonGroup(); group.add(radioButton1); group.add(radioButton2); frame.setLayout(new java.awt.FlowLayout()); frame.add(radioButton1); frame.add(radioButton2); frame.setVisible(true); }); } }
Explanation:
The font, foreground color (text color), and background color of the radio buttons are customized using setFont(), setForeground(), and setBackground().
This allows you to modify the appearance of the radio buttons to fit the overall design of your application.
Conclusion
JRadioButton is an essential component in Java Swing for creating radio buttons, which allow users to select one option from a set of mutually exclusive options.
You can group radio buttons using ButtonGroup, handle selection events with action listeners, and customize the appearance to match your application's design.
Summary of Key Points:
Basic JRadioButton: Use new JRadioButton(“Label”) to create a radio button.
ButtonGroup: Use ButtonGroup to group radio buttons and enforce mutual exclusivity.
Event Handling: Add ActionListener to handle selection events and perform actions when a button is selected.
Pre-selection: Pre-select a radio button using setSelected(true).
Custom ActionListener: Create custom listeners to handle specific behaviors when a radio button is selected.
Custom Appearance: Customize the font, foreground, and background colors of radio buttons for a better user experience.