Master Radio Button in Java with W3Schools Guide

Robert Jul 13, 2026

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.

Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 41
Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 41

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.

How to locate, select & iterate through Radio buttons with Selenium WebDriver & Java
How to locate, select & iterate through Radio buttons with Selenium WebDriver & Java

Creating Radio Buttons Manually

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

W3Schools.com
W3Schools.com

```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

two screens showing the date and time options for an event in order to be recorded
two screens showing the date and time options for an event in order to be recorded

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

30+ CSS Radio Button Styles From CodePen 2018 - Freebie Supply
30+ CSS Radio Button Styles From CodePen 2018 - Freebie Supply

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:

Custom Checkbox & Radio Button With Pure CSS
Custom Checkbox & Radio Button With Pure CSS

```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

jQuery Plugin For Beautifying Checkboxes And Radio Buttons - iCheck
jQuery Plugin For Beautifying Checkboxes And Radio Buttons - iCheck
Radio Buttons UX Design
Radio Buttons UX Design
Pure CSS Custom Radio Buttons with SVG Icons
Pure CSS Custom Radio Buttons with SVG Icons
Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 55
Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 55
HTML Input Types Demo
HTML Input Types Demo
W3Schools.com
W3Schools.com
7 Rules of Using Radio Buttons vs Drop-Down Menus
7 Rules of Using Radio Buttons vs Drop-Down Menus
radio button
radio button
the radio code is shown in this table, and it shows that there are many different types
the radio code is shown in this table, and it shows that there are many different types
Top Features of Java Every Beginner Should Know ☕🚀
Top Features of Java Every Beginner Should Know ☕🚀
JAVA - Student Information System Project In Java Using NetBeans With Source Code | Part 0 - ...
JAVA - Student Information System Project In Java Using NetBeans With Source Code | Part 0 - ...
Bus Reservation System Project in Java with Source Code
Bus Reservation System Project in Java with Source Code
a screen with the text 2 reverse an array description description description description description description description description description description description description description description description description description description description description description description description
a screen with the text 2 reverse an array description description description description description description description description description description description description description description description description description description description description description description description
W3Schools.com
W3Schools.com
Java Switch Example
Java Switch Example
Interface in Java programming language - Codeforcoding
Interface in Java programming language - Codeforcoding
What are Arrays in Java?
What are Arrays in Java?
What is Java?
What is Java?
80+ Pattern Programs In Java
80+ Pattern Programs In Java
Discover interface in java and also know the Difference Between Classes and Interfaces
Discover interface in java and also know the Difference Between Classes and Interfaces

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.