JRadioButton in Java Swing

JRadioButton is a class in Java Swing 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.

The complete example code of JRadioButton 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
JRadioButton in Java Swing 2