CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Swing > JRadioButton in Java Swing

Swing Tutorial

  • What Is Java Swing
  • First Program
  • What is AWT
  • Advantages of Swing Over AWT
  • JFrame
  • Swing Components
  • JLabel
  • JTextField
  • JPasswordField
  • JButton
  • Event Handling
  • FlowLayout
  • BorderLayout
  • GridLayout
  • GridBagLayout
  • BoxLayout
  • JPanel
  • JCheckBox
  • JRadioButton
  • Border Component
  • JComboBox
  • JList
  • JList with MVC
  • Mouse Event
  • Key Event
  • JMenuBar
  • JTextArea
  • Using Color
  • Use Font
  • Display Image/Icon
  • Dialog Box
  • JTable
  • JDesktopPane and JInternalFrame
  • JDesktopPane
  • JInternalFrame
  • Adapter Classes
  • Swing Timer
  • JScrollPane
  • JSlider
  • JProgressBar
  • JSeparator
  • JToolBar
  • ActionListener
  • ItemListener
  • Worker Thread
  • Nimbus Look and Feel

JRadioButton in Java Swing

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 3 min read · 0 Comments
Share: in X

JRadioButton in Java Swing is a class that represents a radio button component. A radio button is a type of button that allows the user to choose one option from a group of mutually exclusive options.

To use JRadioButton in Java Swing, we can follow these steps:

  1. Import the necessary classes:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
  1. Create a JFrame object and set its properties:
JFrame frame = new JFrame("Radio Button Example");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  1. Create a JPanel object to hold the radio buttons:
JPanel panel = new JPanel();
  1. Create the radio buttons and add them to the panel:
JRadioButton maleButton = new JRadioButton("Male");
JRadioButton femaleButton = new JRadioButton("Female");
panel.add(maleButton);
panel.add(femaleButton);
  1. Create a ButtonGroup object and add the radio buttons to it:
ButtonGroup group = new ButtonGroup();
group.add(maleButton);
group.add(femaleButton);
  1. Add the panel to the frame and make it visible:
frame.add(panel);
frame.setVisible(true);

The above code creates a JFrame with two radio buttons labeled “Male” and “Female”. The radio buttons are added to a ButtonGroup so that only one can be selected at a time. The panel containing the radio buttons is then added to the JFrame, which is made visible.

We can add an ActionListener to a JRadioButton to respond to user input. For example, we can create a listener that displays a message when a radio button is selected:

radio1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(null, "Male is selected");
    }
});

This listener displays a message box when the “Male” radio button is selected. We can create similar listeners for the other radio buttons.

Example of JRadioButton in Java Swing

The complete example code of JRadioButton in Java Swing is given below:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class JRadioButtonDemo {
	public static void main(String[] args) {
		// Create a JFrame object and set its properties
		JFrame frame = new JFrame("JRadioButton Demo");
		frame.setSize(300, 200);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Create a JPanel object to hold the radio buttons
		JPanel panel = new JPanel();

		// Create the radio buttons and add them to the panel
		JRadioButton maleButton = new JRadioButton("Male");
		JRadioButton femaleButton = new JRadioButton("Female ");
		panel.add(maleButton);
		panel.add(femaleButton);

		// Create a ButtonGroup object and add the radio buttons to it
		ButtonGroup group = new ButtonGroup();
		group.add(maleButton);
		group.add(femaleButton);

		// Add an ActionListener to the radio buttons
		maleButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, "Male is selected");
			}
		});

		femaleButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, "Female is selected");
			}
		});

		// Add the panel to the frame and make it visible
		frame.add(panel);
		frame.setVisible(true);
	}
}

Output:

JRadioButton in Java Swing

Related Posts:

  • What Is Java Swing? A Complete Guide to Java’s GUI Toolkit
  • JPanel in Java Swing
  • JTable in Java Swing
  • Java Swing Tutorial
  • MySQL Commands for Developers
  • Control Statements in Java
Tags:javaswing
Was this article helpful?
← Previous ArticleJava JSON Serialization and Deserialization
Next Article →Java Thread Lifecycle

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap