Java: Menus
Types of menus
A menu is a way to arrange buttons. There are several types.
- Traditional dropdown menus are positioned across the top of a window in a menu bar, and display below the menu name.
- Popup menus appear when the user clicks, eg with the right mouse button, on a component that can handle a popup request.
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:
- 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.
- JMenu has a name and contains a number of menu itemsl which are displayed is a vertical list of menu items.
- 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:
- Menu mnemonics can be used to open a menu by typing a single character
associated with a menu along with an operating system defined key for this
action. For example, on MS Windows, The ALT key with F will typically open
the File menu. You can then select the relevant menu item with either
the mnemonic key for that item, or with the arrow keys and Enter.
The corresponding letter (
char
) in the menu will be underlined. For examplefileMenu.setMnemonic('F');
- Menu item mnemonics are used to select a menu item when its menu is already open.
Typically the character corresponds to the first, or a significant, letter in the menu item name.
That letter in the menu item will be underlined.
openItem.setMnemonic('O');
- Accelerator key combinations are used to directly invoke a menu item
without opening the menu, for example the
common CTRL-C (Copy) execute the copy menu action.
Accelerator key options are displayed to the right of the menu item name.
Adding accelerator key requires using KeyStroke codes.
There are several ways to get these codes, but the model below shows one of
the easiest.
openItem.setAccelerator(KeyStroke.getKeyStroke("control O"));
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)
- Images, or Images + text in menu items.
- JRadioButtonMenuItem
- JCheckBoxMenuItem
- JPopupMenu in graphics area.
- Submenus
- Actions