# Java: Menus

## Types of menus

Think of a menu as a way to arrange buttons. There are several types.

- Dropdown menus hang down from the menu bar at the top of a window.
- Popup menus appear when the user clicks, eg with the right mouse button, on a component that can handle a popup request.
- Combo boxes are like menus that always shows one item. Similary, there are lists and radio buttons.

## Dropdown menus: JMenuBar, JMenu, and JMenuItem

A menu bar can be added to the top of any top-level containers, 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 is a vertical list of menu items.
3. JMenuItem and Separators are added to each menu. Menu items are usually strings, but can also have icons, checkboxes, or 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.

## Example - MenuDemo

The following program creates a simple menu. It doesn't do anything useful.

## The main program

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
    // File   : menus/MenuDemo.java
    // Purpose: Simple demo of building menus.  main program.
    // Author : Fred Swartz
    // Date   : 2000-04-26 (Rota), 2002-05-01 (Sicilia), 2005-02-08 (Pfalz)

    import javax.swing.*;

    /////////////////////////////////////////////////////////////////// MenuDemo
    public class MenuDemo {
        //================================================================= main
        public static void main(String[] args) {
            JFrame win = new JFrame("MenuDemo");
            win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            //... Create the GUI component (JPanel), which
            //    also contains a method to create a menubar.
            MenuDemoGUI content = new MenuDemoGUI();
            win.setContentPane(content);
            win.setJMenuBar(content.createMenubar());

            win.pack();
            win.setVisible(true);
        }
    }

## The GUI

      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
    // File   : menus/MenuDemoGUI.java
    // Purpose: GUI for menu demo.  Subclasses JPanel and defines a method
    //          which generates menubar.
    // Author : Fred Swartz
    // Date   : 2000-04-26 (Rota), 2002-05-01 (Sicilia), 2005-02-08 (Pfalz)

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

    /////////////////////////////////////////////////////////////// MenuDemoGUI
    class MenuDemoGUI extends JPanel {
        JTextArea m_editArea = new JTextArea(20, 50);

        //... Menuitems are buttons, so should be instance variables
        //    so they can be en-/disabled by various actions.
        JMenuItem m_openItem = new JMenuItem("Open"); // create new menu item
        JMenuItem m_quitItem = new JMenuItem("Quit"); // another menu item
        JMenuItem m_copyItem = new JMenuItem("Copy");
        JMenuItem m_pasteItem= new JMenuItem("Paste");

        //========================================================== constructor
        public MenuDemoGUI() {
            //... Add listeners to menu items
            m_openItem.addActionListener(new OpenAction());
            m_quitItem.addActionListener(new QuitAction());
            //... Copy and Paste don't have listeners yet, so disable them.
            m_copyItem.setEnabled(false);
            m_pasteItem.setEnabled(false);

            //... Add the single component.
            setLayout(new BorderLayout());
            add(m_editArea, BorderLayout.CENTER);
        }

        //==================================================== createMenubar
        // It's common to write a method that produces the menubar.
        // There are two reasons for this.
        // First, it gives a little better organization to the program
        //        and doesn't mix content pane and menu construction.
        // Second, it can be defined wherever you want, so that it can be
        //        put in a JPanel subclass (as here), or elsewhere.
        JMenuBar createMenubar() {
            //... Menubar, menus, menu items.  Use indentation to show structure.
            JMenuBar menubar = new JMenuBar();  // declare and create new menu bar
                JMenu fileMenu = new JMenu("File");// declare and create new menu
                    menubar.add(fileMenu);        // add the menu to the menubar
                    fileMenu.add(m_openItem); // add the menu item to the menu
                    fileMenu.addSeparator();  // add separator line to menu
                    fileMenu.add(m_quitItem);
                JMenu editMenu = new JMenu("Edit");
                    menubar.add(editMenu);
                    editMenu.add(m_copyItem);
                    editMenu.add(m_pasteItem);
            return menubar;
        }

        ///////////////////////////////////////////////////////////// OpenAction
        class OpenAction implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "Sorry, can't open anything");
            }
        }

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