Radio buttons in Java, are a crucial part of any graphical user interface (GUI), enabling users to select a single option from multiple choices. To create radio buttons in Java, we utilize the `JRadioButton` class from the Swing library, which is a powerful tool for building desktop applications. In this article, we'll explore how to use Java radio buttons, along with practical examples.

Java Swing provides various options for creating radio buttons. We can create radio buttons manually using the `JRadioButton` class or use a `ButtonGroup` to group them together for exclusive selection. Let's delve into the details of each approach.

Creating Radio Buttons Manually
To create a single radio button, we use the `JRadioButton` class. Here's a simple example:

```java import javax.swing.*; import java.awt.*; public class RadioButtonExample { public static void main(String[] args) { JFrame frame = new JFrame("Radio Button Example"); JRadioButton radioBtn = new JRadioButton("Option 1"); frame.add(radioBtn); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200); frame.setVisible(true); } } ```
In this example, we create a new JFrame and add a radio button to it using the add() method. The result is a simple window containing a single radio button labeled "Option 1".
Radio Button Initial State

By default, a radio button is not selected. To set the initial state to selected, pass a boolean value of true to the constructor:
```java JRadioButton radioBtn = new JRadioButton("Option 1", true); ```
The radio button will now appear selected when the application runs.
Radio Button Text

To change the text displayed on the radio button, set or modify the text using the setText() method:
```java radioBtn.setText("New Option"); ```
Using ButtonGroup for Exclusive Selection
A ButtonGroup is used to group multiple radio buttons together, ensuring only one can be selected at a time. Here's an example:

```java import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class RadioButtonGroupExample { public static void main(String[] args) { JFrame frame = new JFrame("Radio Button Group Example"); ButtonGroup group = new ButtonGroup(); JRadioButton opt1 = new JRadioButton("Option 1"); JRadioButton opt2 = new JRadioButton("Option 2"); JRadioButton opt3 = new JRadioButton("Option 3"); group.add(opt1); group.add(opt2); group.add(opt3); opt1.addActionListener(e -> System.out.println("Option 1 selected")); opt2.addActionListener(e -> System.out.println("Option 2 selected")); opt3.addActionListener(e -> System.out.println("Option 3 selected")); frame.add(opt1); frame.add(opt2); frame.add(opt3); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200); frame.setLayout(new FlowLayout()); frame.setVisible(true); } } ```
In this example, we create a ButtonGroup and add three radio buttons to it. When we select one radio button, the others become deselected due to the exclusive selection nature of the ButtonGroup. We also add an ActionListener to each radio button to print a message when the button is selected.
Radio Button Positioning




















By default, radio buttons are positioned horizontally. To change the positioning, use a layout manager like FlowLayout or GridLayout to arrange the buttons as desired.
Understanding and implementing radio buttons in Java is a fundamental aspect of developing GUI applications. With the knowledge gained from this article, you can now create interactive and user-friendly forms, questionnaires, and other input tools using radio buttons in your Java projects.