JRadioButtonMenuItemTest.java - Radio Button Menu Item Test Program

This section provides a tutorial example on how to use the javax.swing.JRadioButtonMenuItem class to create multiple radio button menu items and added them into a button group. If one radio button menu item is selected in a group, all others will be unselected.

Other the regular menu item class, javax.swing.JMenuItem, Swing supports a special menu item class, javax.swing.JRadioButtonMenuItem, which represents radio button menu items with following special features:

Here is an example program I wrote to test the JRadioButtonMenuItem classes:

/* JRadioButtonMenuItemTest.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
import javax.swing.*;
public class JRadioButtonMenuItemTest {
   JFrame myFrame = null;
   public static void main(String[] a) {
      (new JRadioButtonMenuItemTest()).test();
   private void test() {
      myFrame = new JFrame("Radio Button Menu Item Test");
      myFrame.setContentPane(new JDesktopPane());

      JMenuBar myMenuBar = new JMenuBar();
      JMenu myMenu = getFileMenu();
      myMenu = getColorMenu();
      myMenu = getOptionMenu();
      JMenuItem myItem = new JMenuItem("Help");

   private JMenu getFileMenu() {
      JMenu myMenu = new JMenu("File");
      JMenuItem myItem = new JMenuItem("Open");
      myItem = new JMenuItem("Close");
      myItem = new JMenuItem("Exit");
      return myMenu;
   private JMenu getColorMenu() {
      JMenu myMenu = new JMenu("Color");
      ButtonGroup myGroup = new ButtonGroup();
      JRadioButtonMenuItem myItem = new JRadioButtonMenuItem("Red");
      myItem = new JRadioButtonMenuItem("Green");
      myItem = new JRadioButtonMenuItem("Blue");
      return myMenu;
   private JMenu getOptionMenu() {
      JMenu myMenu = new JMenu("Option");
      JMenuItem myItem = new JMenuItem("Sound");
      myItem = new JMenuItem("Auto save");
      return myMenu;

If you run this example, you will see the frame window shows up with the menu bar like this:

Radio Button as Menu Item (on macOS)
Radio Button as Menu Item (on macOS)

Interesting notes about this tutorial example:

There seems to be a problem with radio button menu items on macOS computers. They are dislayed without the radio button icons. If you run the same program on a Windows computer, you will see radio button icons displayed as shown below.

Radio Button Menu Item Test (on Windows)
Radio Button Menu Item Test (on Windows)

Table of Contents

 About This Book

 JDK (Java Development Kit)

 Introduction of Java Swing Package

 Graphics Environment of the Local System

 JFrame - Main Frame Class

 JLabel - Swing Label Class

 JButton - Swing Button Class

 JCheckBox - Swing Check Box Class

 JRadioButton - Swing Radio Button Class

 JTextField - Swing Text Field Class

 JComboBox - Swing Combo Box Class

Menu Bar, Menus, Menu Items and Listeners

 JMenuBar, JMenu, and JMenuItem Classes

 JMenuBarTest.java - Menu Bar Test Program

 JMenuTest.java - Menu Test Program

 JMenuItemTest.java - Menu Item Test Program

JRadioButtonMenuItemTest.java - Radio Button Menu Item Test Program

 JCheckBoxMenuItemTest.java - Check Box Menu Item Test Program

 javax.swing.event.MenuListener - Menu Listener Interface

 JMenuItemActionListenerTest.java - Menu Item Action Listener Test

 Item Listener on Radio Button Menu Items

 Item Listener on Check Box Menu Items

 javax.swing.event.MenuKeyListener - Menu Key Listener Interface

 setMnemonic() - Setting Keyboard Mnemonics on Menu Items

 setAccelerator() - Setting Keyboard Accelerators on Menu Items

 setMnemonic() - Setting Keyboard Mnemonics on Menus

 Creating Internal Frames inside the Main Frame

 Layout of Components in a Container

 LookAndFeel and UIManager

 Option Dialog Boxes

 JEditorPane - The Editor Pane Class

 SwingWorker - The Background Task Worker

 AWT (Abstract Windows Toolkit)

 Integration with Desktop System

 Outdated Tutorials


 Full Version in PDF/EPUB