Creating Radio Buttons with JRadioButton

In this tutorial, you will learn how to use JRadionButton class to create radio buttons.

Radio buttons allow the user to make a single choice from a set of options. To create a radio button, you use JRadioButton class.

The radio buttons are often used in a group to display multiple options, therefore, they are used with ButtonGroup class. The ButtonGroup enforces only one button in a group is selected at a given time. The ButtonGroup does not have the visual appearance. It is just used to hold logical grouping sets of buttons.

Here are the most common used constructors of JRadioButton class:

JRadioButton ConstructorsMeaning
public JRadioButton( )Creates a radio button without text and icon. The state of the radio button is not selected.
public JRadioButton(Icon icon)Creates a radio button with an icon.
public JRadioButton(Icon icon, boolean selected)Creates a radio button with an icon. The selection state of the radio button is indicated by the boolean parameter selected.
public JRadioButton(String text)Create a radio button with text.
public JRadioButton(String text, boolean selected)Create a radio button with text and initialize the state of the radio button.
public JRadioButton(String text, Icon icon)Creates a radio button which displays both text and icon.
public JRadioButton(String text, Icon icon, boolean selected)Creates a radio button which displays both text and icon. The state of radio button can be initialized.

Like a checkbox, you can handle the state of a radio button using ActionListener or ItemListener.

Example of using JRadioButton and ButtonGroup

In this example, we will create three radio buttons and add them to a button group. When the user selects a radio button, a message will pop up to notify the button user clicks and state of selected radio button.

JRadioButton - Radio Button in Swing
package jradiobuttondemo; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Main { public static void main(String[] args) { final JFrame frame = new JFrame("JRadioButton Demo"); // implement ItemListener interface class MyItemListener implements ItemListener { public void itemStateChanged(ItemEvent ev) { boolean selected = (ev.getStateChange() == ItemEvent.SELECTED); AbstractButton button = (AbstractButton) ev.getItemSelectable(); String command = button.getActionCommand(); // build message String state; if (selected) { state = "selected"; } else { state = "unselected"; } String message; if (command.equals("SWING")) { message = "The Swing option has been " + state; } else if (command.equals("AWT")) { message = "The AWT option has been " + state; } else { message = "The APPLET option has been " + state; } // show dialog JOptionPane.showMessageDialog(frame, message); } } // creates radio button and set corresponding action // commands JRadioButton rdbApplet = new JRadioButton("Java Applet"); rdbApplet.setActionCommand("APPLET"); JRadioButton rdbJavaAWT = new JRadioButton("Java AWT"); rdbJavaAWT.setActionCommand("AWT"); JRadioButton rdbJavaSwing = new JRadioButton("Java Swing"); rdbJavaSwing.setActionCommand("SWING"); // add event handler MyItemListener myItemListener = new MyItemListener(); rdbApplet.addItemListener(myItemListener); rdbJavaAWT.addItemListener(myItemListener); rdbJavaSwing.addItemListener(myItemListener); // add radio buttons to a ButtonGroup final ButtonGroup group = new ButtonGroup(); group.add(rdbApplet); group.add(rdbJavaAWT); group.add(rdbJavaSwing); // Frame setting frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200); Container cont = frame.getContentPane(); cont.setLayout(new GridLayout(0, 1)); cont.add(new JLabel("Please choose your favorite Java stuff:")); cont.add(rdbApplet); cont.add(rdbJavaAWT); cont.add(rdbJavaSwing); frame.setVisible(true); } }
Code language: JavaScript (javascript)