Java: Menus

Types of menus

A menu is a way to arrange buttons. There are several types.

Dropdown menus: JMenuBar, JMenu, and JMenuItem

A menu bar can be added to the top of a top-level container, eg, JFrame, JApplet, or JDialog. Note that a menu bar can not be added to JPanel.

Dropdown menus have three parts:

  1. JMenuBar is positioned across the top of a container (eg a JFrame, JPanel, or JApplet). It's placed above the content pane, so does not use the container's layout. Add menus to the menubar.
  2. JMenu has a name and contains a number of menu itemsl which are displayed is a vertical list of menu items.
  3. JMenuItems and Separators are added to each menu. Menu items are usually text "buttons", but can also have icons, checkboxes, radio buttons, or be hierarchical submenus.

Menus and Menu Items are Buttons!

It is easy to see how menu items are buttons that appear when a menu appears. But the menu names in the menu bar are also buttons. When you press on these "buttons", they create a popup menu that you see as a dropdown menu.

Keyboard Mnemonics and Accelerators

You can associated characters with menus and menu items so that the user can invoke them from the keyboard:

Example - MenuDemo

The source code

The following program creates some simple menus. It doesn't do anything useful.

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
 64 
 65 
 66 
 67 
 68 
 69 
 70 
 71 
 72 
 73 
 74 
 75 
 76 
 77 
 78 
 79 
 80 
 81 
 82 
 83 
 84 
 85 
 86 
 87 
 88 
 89 
 90 
 91 
 92 
 93 
 94 
 95 
 96 
 97 
// File   : gui/components/menus/MenuDemo.java
// Purpose: GUI for menu demo.  Subclasses JFrame.
//          Shows dropdown and popup menus, mnemonic and accelerator keys.
// Author : Fred Swartz - Placed in public domain.
// Date   : 2000-04-26 (Rota), 2002-05-01 (Sicilia), 2006-10-04 (Pfalz)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

////////////////////////////////////////////////////////////////// MenuDemo
class MenuDemo extends JFrame {
    //============================================================== fields
    private JTextArea m_editArea = new JTextArea(20, 50);

    //... Menuitems are instance variables when they are referenced
    //    in a listener, eg, to en-/disabled them.
    private JMenuItem m_copyItem;
    private JMenuItem m_pasteItem;

    private JPopupMenu m_popup = new JPopupMenu();

    //========================================================== constructor
    public MenuDemo() {
        // (1) Create menu items and set their mnemonic, accelerator, enabled.
        m_copyItem = new JMenuItem("Copy");
            m_copyItem.setEnabled(false);
            m_copyItem.setAccelerator(KeyStroke.getKeyStroke("control C"));
        m_pasteItem = new JMenuItem("Paste");
            m_pasteItem.setEnabled(false);
            m_pasteItem.setAccelerator(KeyStroke.getKeyStroke("control V"));
        JMenuItem openItem = new JMenuItem("Open...");
            openItem.setMnemonic('O');
            openItem.setAccelerator(KeyStroke.getKeyStroke("control O"));
        JMenuItem quitItem = new JMenuItem("Quit");
            quitItem.setMnemonic('Q');
            quitItem.setAccelerator(KeyStroke.getKeyStroke("control Q"));

        // (2) Build  menubar, menus, and add menuitems.
        JMenuBar menubar = new JMenuBar();  // Create new menu bar
            JMenu fileMenu = new JMenu("File"); // Create new menu
                fileMenu.setMnemonic('F');
                menubar.add(fileMenu);      // Add menu to the menubar
                fileMenu.add(openItem);     // Add menu item to the menu
                fileMenu.addSeparator();    // Add separator line to menu
                fileMenu.add(quitItem);
            JMenu editMenu = new JMenu("Edit");
                fileMenu.setMnemonic('E');
                menubar.add(editMenu);
                editMenu.add(m_copyItem);
                editMenu.add(m_pasteItem);

        // (3) Add listeners to menu items
        openItem.addActionListener(new OpenAction());
        quitItem.addActionListener(new QuitAction());


        //... Add the (unused) text area to the content pane.
        JPanel content = new JPanel();
        content.setLayout(new BorderLayout());
        content.add(m_editArea, BorderLayout.CENTER);

        //... Add menu items to popup menu, add popup menu to text area.
        m_popup.add(new JMenuItem("Testing"));
        m_editArea.setComponentPopupMenu(m_popup);

        //... Set the JFrame's content pane and menu bar.
        setContentPane(content);
        setJMenuBar(menubar);

        setTitle("Menu Demo");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);  // Center window.
    }

    ///////////////////////////////////////////////////////////// OpenAction
    class OpenAction implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(MenuDemo.this, "Can't Open.");
        }
    }

    ///////////////////////////////////////////////////////////// QuitAction
    class QuitAction implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);  // Terminate the program.
        }
    }

    //================================================================= main
    public static void main(String[] args) {
        JFrame win = new MenuDemo();
        win.setVisible(true);
    }
}

Issues not covered (yet)