Java Swing Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.01

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:

  • A radio button menu item will be listed with a radio button icon.
  • The radio button icon will be displayed as checked if the radio button menu item is selected.
  • The radio button icon will be displayed as unchecked if the radio button menu item is unselected.
  • Multiple radio button menu items are added into a ButtonGroup object to form button group.
  • If one radio button menu item in a button group is selected, all other radio button menu items will be unselected.

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

/**
 * JRadioButtonMenuItemTest.java
 * Copyright (c) 2009 by Dr. Herong Yang, http://www.herongyang.com/
 */
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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      myFrame.setBounds(50,50,250,150);
      myFrame.setContentPane(new JDesktopPane());

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

      myFrame.setJMenuBar(myMenuBar);
      myFrame.setVisible(true);
   }
   private JMenu getFileMenu() {
      JMenu myMenu = new JMenu("File");
      JMenuItem myItem = new JMenuItem("Open");
      myMenu.add(myItem);
      myItem = new JMenuItem("Close");
      myItem.setEnabled(false);
      myMenu.add(myItem);
      myMenu.addSeparator();
      myItem = new JMenuItem("Exit");
      myMenu.add(myItem);
      return myMenu;
   }
   private JMenu getColorMenu() {
      JMenu myMenu = new JMenu("Color");
      ButtonGroup myGroup = new ButtonGroup();
      JRadioButtonMenuItem myItem = new JRadioButtonMenuItem("Red");
      myItem.setSelected(true);
      myGroup.add(myItem);
      myMenu.add(myItem);
      myItem = new JRadioButtonMenuItem("Green");
      myGroup.add(myItem);
      myMenu.add(myItem);
      myItem = new JRadioButtonMenuItem("Blue");
      myGroup.add(myItem);
      myMenu.add(myItem);
      return myMenu;
   }
   private JMenu getOptionMenu() {
      JMenu myMenu = new JMenu("Option");
      JMenuItem myItem = new JMenuItem("Sound");
      myMenu.add(myItem);
      myItem = new JMenuItem("Auto save");
      myMenu.add(myItem);
      return myMenu;
   }
}

If you run this example, you will see the frame window shows up with the menu bar like this:
Radio Button Menu Item Test

Interesting notes about this tutorial example:

  • A radio button menu item can be listed as selected by default, if the setSelected(true) method is called on the item. The "Red" radio button menu item selected by default in this example.

Sample programs listed in this section have been tested with JDK 1.6.0.

Last update: 2009.

Table of Contents

 About This Java Swing Tutorial Book

 Introduction of Java Swing Package

 Graphics Environment of the Local System

 JFrame - Main Frame Class

 JLabel - Swing Label Class

 JButton - Swing Button Class

 JRadioButton - Swing Radio Button Class

 JTextField - Swing Text Field 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

 MenuItemActionListenerTest.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

 References

 Printable Copy - PDF Version

Dr. Herong Yang, updated in 2009
JRadioButtonMenuItemTest.java - Radio Button Menu Item Test Program