This document describes the API for MenuBars in Swing.
The MenuBar class is a lightweight version of the AWT MenuBar. 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.
For more information about MenuBars and their participation in the Menu
system as a whole, see the specification titled The
Swing Menu System.
Features
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.
MVC
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.
Events
Menu bars are sources for ComponentEvents and ContainerEvents.
The 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.
-
- add(Component)
- Adds the specified component to the menu bar.
-
- add(JMenuItem)
- Adds the specified MenuItem 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.
-
- getMenuItem(int)
- Gets the specified MenuItem.
-
- 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.
-
- remove(Component)
- Removes the specified Component from the menu bar.
-
- remove(JMenu)
- Removes the specified Menu from the menu bar.
-
- remove(JMenuItem)
- Removes the specified MenuItem from the menu bar.
-
- setHelpMenu(JMenu)
- Sets the help menu to the specified menu on the menu bar.
-
- shortcuts()
- Gets an Enumeration of all MenuShortcuts this MenuBar manages.
-
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.4. Last modified 09/04/97.
Copyright © 1995-97 Sun
Microsystems, Inc. All Rights Reserved.