![]() ![]() |
![]() |
![]() ![]() ![]() ![]() |
![]() |
The Swing menu system is designed as a superset of the AWT menu system. The changes that have been made are designed to take advantage of the capabilities of lightweight components while simplifying the menu class hierarchy and increasing maintainability. This document describes the design of the Swing menu system as a whole.
Comprehensive documentation for the Swing menu system is currently under development. In this document, the individual components of the Swing menu system are briefly described in detail in their own specifications. Consult the menu-related JavaDoc APIs for current details.
The 1.1 AWT provides a simple but fairly restrictive set of capabilities for menus. In most cases, the restrictions were necessary because of restrictions in the native windowing systems' menu controls. The basis of the 1.1 AWT menu class hierarchy is the abstract superclass MenuComponent. Various problem arise as a result of this design:
The goals of the Swing menu design were:
The section describes the menu system at a high level. For specific information on individual component classes, see their respective documents.
A set of menus is usually used in an application to give the end user a kind of "table of contents" for actions that are available to him or her. A menu bar is most often displayed at the top of the application's main window (JFrame). It presents a set of menus that represent the main categories or groupings of actions. The actions themselves are usually presented as menu items on the menus, but in some cases they are grouped together on submenus that are subordinate to the top-level menus. The Swing menu system is made up of several component classes:
These classes are used to create a menu hierarchy. This can be thought of in terms of a containment hierarchy, but has one important difference: Menus contain their children in a separate physical window. This arrangement tends to wreak havoc on our traditional ideas of what happens in the containment relation. At the same time, it is extremely useful to deal with the menu hierarchy as if it were a containment hierarchy.
Creating menus in Swing is very similar to creating menus using AWT. This example shows a simple case in which AWT code has been converted to Swing by adding a "J" to the menu class names:
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Roman");
menuBar.add(menu);
menu.add(new JMenuItem("Caesar"));
menu.add(new JMenuItem("Catullus"));
JMenu test2 = (JMenu) menu.add(new JMenu("Horace"));
test2.add(new JCheckboxMenuItem("Odes"));
test2.add(new JCheckboxMenuItem("Epodes"));
menu = new JMenu("Swedish");
menuBar.add(menu);
menu.add(new JMenuItem("Per Lagerkvist"));
menu.add(new JMenuItem("Selma Lagerlöf"));
menu.add(new JMenuItem("Åsa Moberg"));
menu.addSeparator();
JMenuItem skeelos = menu.add(new JMenuItem("Jojje"));
menuBar.add(menu);
With the additional capabilities of Swing components, it is possible to add images to menu items:
Icon georges = new ImageIcon("images/georges.gif");
JMenuItem skeelos = menu.add(new JMenuItem("Jojje", georges));
skeelos.setHorizontalTextPosition(JButton.RIGHT);
skeelos.setHorizontalAlignment(JButton.LEFT);
skeelos.setVerticalTextPosition(JButton.CENTER);
Version 0.5. Last modified 09/30/97.
Copyright © 1995-97 Sun
Microsystems, Inc. All Rights Reserved.