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

JMenuItemTest.java - Menu Item Test Program

This section provides a tutorial example on how to use the javax.swing.JMenuItem class to create menu items and add them to different menus, sub menus, or the menu bar. Sub menus and menu items added to a menu will be listed vertically when the menu is selected.

Here is an example program I wrote to test the JMenuItem class:

/**
 * JMenuItemTest.java
 * Copyright (c) 2009 by Dr. Herong Yang, http://www.herongyang.com/
 */
import javax.swing.*;
public class JMenuItemTest {
   JFrame myFrame = null;
   public static void main(String[] a) {
      (new JMenuItemTest()).test();
   }
   private void test() {
      myFrame = new JFrame("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");
      JMenuItem myItem = new JMenuItem("Red");
      myMenu.add(myItem);
      myItem = new JMenuItem("Green");
      myMenu.add(myItem);
      myItem = new JMenuItem("Blue");
      myMenu.add(myItem);
      return myMenu;
   }
   private JMenu getOptionMenu() {
      JMenu myMenu = new JMenu("Option");
      JMenu mySubMenu = getOnOffMenu("Sound");
      myMenu.add(mySubMenu);
      mySubMenu = getOnOffMenu("Auto save");
      mySubMenu.setEnabled(false);
      myMenu.add(mySubMenu);
      return myMenu;
   }
   private JMenu getOnOffMenu(String title) {
      JMenu myMenu = new JMenu(title);
      JMenuItem myItem = new JMenuItem("On");
      myMenu.add(myItem);
      myItem = new JMenuItem("Off");
      myMenu.add(myItem);
      return myMenu;
   }
}

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

Interesting notes about this tutorial example:

  • A menu item can be added directly on the menu bar. The "Help" menu item is added to the menu bar in this example. But it is strongly not recommended. You will get a crash if you use the arrow key to navigate in the menu bar.
  • A menu item can be disabled by calling the set setEnabled(false) function. A disabled menu item will be listed in gray and not be clickable. The "Close" menu item is disabled in this example.
  • A menu can also be disabled by calling the set setEnabled(false) function. A disabled menu will be listed in gray and not be clickable. The "Auto save" menu item is disabled in this example.

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

Last update: 2009.

Sections in This Chapter

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

Dr. Herong Yang, updated in 2009
JMenuItemTest.java - Menu Item Test Program