
This document describes the API for MenuItems in Swing.
In AWT 1.1.x, MenuItem was a subclass of MenuComponent, which
was subclassed directly from Object. This architecture meant that improvements
to Component and Container were not automatically reusable in MenuItem,
but first had to be duplicated in MenuComponent.
This was necessary in 1.1.x because Menus were based on native
controls, which in many cases have rather severe limitations on MenuItems.
In the Swing toolkit, implementing Menus and MenuItems as lightweight components
frees us from many of these limitations. For instance, we can now decide
to add any type of Component to Menus.
A MenuItem is basically a button on a popup menu. This arrangement provides
a look and feel that is slightly different from a non-menu button. Because
most applications have a common idea of what a MenuItem's look and feel
should be, it makes sense to include this class in the toolkit to support
consistency across applications. This strategy also provides a common point
for defining the look and feel for consistency with the native toolkits.
Features
Features of the Swing MenuItem API include the following:
- A MenuItem is subclass of abstract button
- A MenuItem is an Action Event Source.
- Association with Actions (not yet included in 0.3).
- Shortcuts(not yet included in 0.3) .
- The ability to support help mechanisms (status bar info) (not yet included
in 0.3).
Goals
The goals for the MenuItem design are:
- To provide an API for MenuItem which allows developers to take advantage
of the flexibility of lightweight components.
- To retain compatibility with the 1.1.x APIs to the best extent
possible.
MVC
The Model for MenuItem is directly inherited from Button. In fact, only
the view and controllers are modified to provide the proper L&F for
a button on a Menu. The component itself has one addition, which is the
ability to be associated with a Command.
The API
The API for MenuItems is a superset of the 1.1.1 API. In Swing, MenuItem
is a subclass of Button. This means that MenuItem is included in the normal
Component hierarchy rather than belonging to a separate MenuComponent hierarchy.
For this reason, many of the methods that had to be defined by MenuItem
are now received through inheritance from Button or Component. Where there
is a need for different behavior in MenuItems from the superclass, these
methods are overridden.
Although there is no technical need for a CheckboxMenuItem class, we
may decide to create one. If we do, it will be merely a subclass of CheckBox
-- a strategy that makes sense for compatibility reasons.
The following methods are included in the MenuItem API:
- MenuItem()
- Constructs a new MenuItem with an empty label and no keyboard shortcut.
-
- MenuItem(String)
- Constructs a new MenuItem with the specified label and no keyboard
shortcut.
-
- MenuItem(String, MenuShortcut)
- Creates a MenuItem with an associated keyboard shortcut.
-
- MenuItem(String, MenuShortcut, Menu)
- Creates a MenuItem with an associated keyboard shortcut and a given
submenu.
-
- deleteShortcut()
- Deletes any MenuShortcut associated with this MenuItem.
-
- getShortcut()
- Returns the MenuShortcut associated with this MenuItem, or null if
none has been specified.
-
- paramString()
- Returns the String parameter of the menu item.
-
- processActionEvent(ActionEvent)
- Processes action events occurring on this menu item by dispatching
them to any registered ActionListener objects.
-
- setShortcut(MenuShortcut)
- Set this MenuItem's MenuShortcut.
-
- setCommand(Command)
- Sets the command to be invoked when the MenuItem is selected.
-
- Command getCommand()
- Returns the Command which is invoked when the MenuItem is selected.
Deprecated Method
The following method is deprecated in the MenuItem API:
- addNotify()
- Creates the menu item's peer.
Methods No Longer Needed
The following methods were inherited from Button asf are no longer needed:
- addActionListener(ActionListener)
- Adds the specified action listener to receive action events from this
menu item.
-
- disable()
- Deprecated.
-
- disableEvents(long)
- Disables the events defined by the specified event mask parameter from
being delivered to this menu item.
-
- enable()
- Deprecated.
-
- enable(boolean)
- Deprecated.
-
- enableEvents(long)
- Enables the events defined by the specified event mask parameter to
be delivered to this menu item.
-
- getActionCommand()
- Returns the command name of the action event fired by this menu item.
-
- getLabel()
- Gets the label for this menu item.
-
- isEnabled()
- Checks whether the menu item is enabled.
-
- processEvent(AWTEvent)
- Processes events on this menu item
-
- removeActionListener(ActionListener)
- Removes the specified action listener so it no longer receives action
events from this menuitem.
-
- setActionCommand(String)
- Sets the command name of the action event fired by this menu item.
- setEnabled(boolean)
- Sets whether or not this menu item can be chosen.
-
- setLabel(String)
- Sets the label to be the specified label.
Questions
At this point in the development of Swing, the following questions are
still being considered (and developer feedback is appreciated):
- Should the association with Commands be promoted to the level of Buttons?
- Do MenuItems and Menus traverse the normal Container hierarchy or use
another set of APIs (for example, a submenu/supermenu API set, or APIs
dealing with child menus and parent menus)?

Version 0.4. Last modified 09/04/97.
Copyright © 1995-97 Sun
Microsystems, Inc. All Rights Reserved.