JMenuTest.java - Menu Test Program

This section provides a tutorial example on how to use the javax.swing.JMenu class to create multiple menus and how to add them to 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 JMenu class:

/* JMenuTest.java
 * Copyright (c) 1997-2024 HerongYang.com. All Rights Reserved.
 */
import javax.swing.*;
public class JMenuTest {
   JFrame myFrame = null;
   public static void main(String[] a) {
      (new JMenuTest()).test();
   }
   private void test() {
      myFrame = new JFrame("Menu 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);

      myFrame.setJMenuBar(myMenuBar);
      myFrame.setVisible(true);
   }
   private JMenu getFileMenu() {
      JMenu myMenu = new JMenu("File");
      JMenu mySubMenu = getOpenMenu();
      myMenu.add(mySubMenu);
      JMenuItem myItem = new JMenuItem("Close");
      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 getOpenMenu() {
      JMenu myMenu = new JMenu("Open");
      JMenuItem myItem = new JMenuItem("Java");
      myMenu.add(myItem);
      myItem = new JMenuItem("HTML");
      myMenu.add(myItem);
      myItem = new JMenuItem("GIF");
      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 with Two Levels
Menu with Two Levels

Interesting notes about this tutorial example:

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

 Archived Tutorials

 References

 Full Version in PDF/EPUB