Contents

Swinging Duke
Feedback Button
Left ArrowRight Arrow

 

The Swing MenuItem API

This document briefly describes the API for MenuItems in Swing. This document briefly describes the JMenuItem 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.


About the JMenuItem Class

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 place severe limitations on MenuItems. In the Swing toolkit, implementing JMenu and JMenuItem as lightweight component classes frees us from many of these limitations. For instance, we can now decide to add any type of component to a menu.

A JMenuItem 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 JMenuItem'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 JMenuItem API include the following:


Goals

The goals for the Swing JMenuItem design were:

  1. To provide an API for JMenuItem that allows developers to take advantage of the flexibility of lightweight components.
  2. To retain compatibility with the 1.1.x APIs to the best extent possible.


The MenuItem API

The following methods are included in the JMenuItem API:

JMenuItem()
Constructs a new MenuItem with an empty label and no keyboard shortcut.
 
JMenuItem(String)
Constructs a new MenuItem with the specified label and no keyboard shortcut.
 
deleteShortcut()
Deletes any MenuShortcut associated with this MenuItem.
 
getShortcut()
Returns the MenuShortcut associated with this MenuItem, or null if none has been specified.
 
setShortcut(MenuShortcut)
Set this MenuItem's MenuShortcut.
 


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.


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

Sun's Home Page