This document briefly describes the JMenuBar
class, which is used to create menu bars in Swing. Comprehensive documentation
for the Swing menu system is currently under development. For general information
about menus and how they work in the Menu system as a whole, see the specification
titled The Swing Menu System. Consult the
menu-related JavaDoc APIs for current details.
Features of JMenuBar
JMenuBar is a lightweight version of the AWT MenuBar class. It is a JComponent
that can contain JMenus or other components. It is most often used to display
a table of contents for the actions available in an application.Features
of the Swing MenuBar mechanism include:
- Help menus (not yet supported in 0.3) This feature will make it possible
to designate a JMenu as the "help menu" for a menu bar. Selected
look and feels may decide that the help menu should be displayed in a special
way, such as flush against the right margin.
- Multiple rows (not yet supported in 0.3) It should be possible to set
the behavior of the menu bar when there is not enough space to display
all of its components on one row.
The MenuBar Model
The MenuBar class uses a SingleSelectionModel. This means that only one
of its children can be selected at a time. It is possible to listen for
changes in the current selection by getting and listening to the menu bar's
model.
The MenuBar API
The API for JMenuBar is a superset of the 1.1.1 API. It includes the
following methods:
- add(JMenu)
- Adds the specified menu to the menu bar.
-
- deleteShortcut(JMenuShortcut)
- Deletes the specified MenuShortcut.
-
- getHelpMenu()
- Gets the help menu on the menu bar.
-
- getMenu(int)
- Gets the specified menu. Calls throught the MenuItem at the given index.
-
- getMenuCount()
- Counts the number of menus on the menu bar. (MenuItems) .
-
- getShortcutMenuItem(JMenuShortcut)
- Return the MenuItem associated with a MenuShortcut, or null if none
has been specified.
-
- remove(int)
- Removes the menu located at the specified index from the menu bar.
Actually removes the MenuItem at the index.
-
- setHelpMenu(JMenu)
- Sets the help menu to the specified menu on the menu bar.
-
Deprecated Methods
The following methods are deprecated in the Swing MenuBar API:
- remove(MenuComponent)
- Removes the specified MenuComponent from the MenuBar.
-
- addNotify()
- Creates the menu bar's peer.
-
- countMenus()
- Deprecated.
-
- removeNotify()
- Removes the menu bar's peer.

Version 0.5. Last modified 10/1/97.
Copyright © 1995-97 Sun
Microsystems, Inc. All Rights Reserved.