We started this chapter by introducing the most common components that you might want to place into a window, such as various kinds of buttons, text fields, and combo boxes. Swing also supports another type of user interface elements, the pull-down menus that are familiar from GUI apps. A menu bar on top of the window contains the names of the pull-down menus. Clicking on a name opens the menu containing menu items and submenus. When the user clicks on a menu item, all menus are closed and a message is sent to the program. shows a typical menu with a submenu.

A menu with a submenu

Java graphics 09fig22

Building Menus

Building menus is straightforward. You create a menu bar:

JMenuBar menuBar = new JMenuBar();

A menu bar is just a component that you can add anywhere you like. Normally, you want it to appear at the top of a frame. You can add it there with the setJMenuBar method:

frame.setJMenuBar(menuBar);

For each menu, you create a menu object:

JMenu editMenu = new JMenu("Edit");

You add the top-level menus to the menu bar:

menuBar.add(editMenu);

You add menu items, separators, and submenus to the menu object:

JMenuItem pasteItem = new JMenuItem("Paste");
editMenu.add(pasteItem);
editMenu.addSeparator();
JMenu optionsMenu = . . .; // a submenu editMenu.add(optionsMenu);

When the user selects a menu, an action event is triggered. You need to install an action listener for each menu item.

ActionListener listener = . . .;
pasteItem.addActionListener(listener);

There is a convenient method JMenu.add(String s) that adds a menu item to the end of a menu, for example:

editMenu.add("Paste");

The add method returns the created menu item, so that you can capture it and then add the listener, as follows:

JMenuItem pasteItem = editMenu.add("Paste");
pasteItem.addActionListener(listener);

It often happens that menu items trigger commands that can also be activated through other user interface elements such as toolbar buttons. In , you saw how to specify commands through Action objects. You define a class that implements the Action interface, usually by extending the AbstractAction convenience class. You specify the menu item label in the constructor of the AbstractAction object, and you override the actionPerformed method to hold the menu action handler. For example,

Action exitAction = new
 AbstractAction("Exit") // menu item text goes here
 {
 public void actionPerformed(ActionEvent event)
 {
 // action code goes here
 System.exit(0);
 }
 };

You can then add the action to the menu:

JMenuItem exitItem = fileMenu.add(exitAction);

This command adds a menu item to the menu, using the action name. The action object becomes its listener. This is just a convenient shortcut for

JMenuItem exitItem = new JMenuItem(exitAction);
fileMenu.add(exitItem);

Java graphics notes_icon

In Windows and Macintosh programs, menus are generally defined in an external resource file and tied to the app with resource identifiers. It is possible to build menus programmatically, but it is not commonly done. In Java, menus are still usually built inside the program because the mechanism for dealing with external resources is far more limited than it is in Windows or Mac OS.

javax.swing.JMenu 1.2

Java graphics api_icon