This documentation differs from the official API. Jadeite adds extra features to the API including: variable font sizes, constructions examples, placeholders for classes and methods, and auto-generated “See Also” links. Additionally it is missing some items found in standard Javadoc documentation, including: generics type information, “Deprecated” tags and comments, “See Also” links, along with other minor differences. Please send any questions or feedback to bam@cs.cmu.edu.


java.awt
class MenuItem

java.lang.Object extended by java.awt.MenuComponent extended by java.awt.MenuItem
All Implemented Interfaces:
Serializable, Accessible
Direct Known Subclasses:
CheckboxMenuItem, Menu

Most common way to construct:

MenuItem mi = new MenuItem("Exit");

Based on 102 examples


public class MenuItem
extends MenuComponent
implements Accessible

All items in a menu must belong to the class MenuItem, or one of its subclasses.

The default MenuItem object embodies a simple labeled menu item.

This picture of a menu bar shows five menu items: The following text describes this graphic.
The first two items are simple menu items, labeled "Basic" and "Simple". Following these two items is a separator, which is itself a menu item, created with the label "-". Next is an instance of CheckboxMenuItem labeled "Check". The final menu item is a submenu labeled "More Examples", and this submenu is an instance of Menu.

When a menu item is selected, AWT sends an action event to the menu item. Since the event is an instance of ActionEvent, the processEvent method examines the event and passes it along to processActionEvent. The latter method redirects the event to any ActionListener objects that have registered an interest in action events generated by this menu item.

Note that the subclass Menu overrides this behavior and does not send any event to the frame until one of its subitems is selected.


Nested Class Summary
protected class

           Inner class of MenuItem used to provide default support for accessibility.
Nested classes/interfaces inherited from class java.awt.MenuComponent
MenuComponent.AccessibleAWTMenuComponent
   
Constructor Summary

          Constructs a new MenuItem with an empty label and no keyboard shortcut.

          Constructs a new MenuItem with the specified label and no keyboard shortcut.

          Create a menu item with an associated keyboard shortcut.
 
Method Summary
 void

          Adds the specified action listener to receive action events from this menu item.
 void

          Creates the menu item's peer.
 void

          Delete any MenuShortcut object associated with this menu item.
 void

          
protected void
disableEvents(long eventsToDisable)

          Disables event delivery to this menu item for events defined by the specified event mask parameter.
 void

          
 void
enable(boolean b)

          
protected void
enableEvents(long eventsToEnable)

          Enables event delivery to this menu item for events to be defined by the specified event mask parameter
 AccessibleContext

          Gets the AccessibleContext associated with this MenuItem.
 String

          Gets the command name of the action event that is fired by this menu item.
 ActionListener[]

          Returns an array of all the action listeners registered on this menu item.
 String

          Gets the label for this menu item.
 EventListener[]
getListeners(Class listenerType)

          Returns an array of all the objects currently registered as FooListeners upon this MenuItem.
 MenuShortcut

          Get the MenuShortcut object associated with this menu item,
 boolean

          Checks whether this menu item is enabled.
 String

          Returns a string representing the state of this MenuItem.
protected void

          Processes action events occurring on this menu item, by dispatching them to any registered ActionListener objects.
protected void

          Processes events on this menu item.
 void

          Removes the specified action listener so it no longer receives action events from this menu item.
 void

          Sets the command name of the action event that is fired by this menu item.
 void
setEnabled(boolean b)

          Sets whether or not this menu item can be chosen.
 void

          Sets the label for this menu item to the specified label.
 void

          Set the MenuShortcut object associated with this menu item.
 
Methods inherited from class java.awt.MenuComponent
dispatchEvent, getAccessibleContext, getFont, getName, getParent, getPeer, getTreeLock, paramString, postEvent, processEvent, removeNotify, setFont, setName, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MenuItem

public MenuItem()
         throws HeadlessException
Constructs a new MenuItem with an empty label and no keyboard shortcut.

Throws:
HeadlessException - if GraphicsEnvironment.isHeadless() returns true.

MenuItem

public MenuItem(String label)
         throws HeadlessException
Constructs a new MenuItem with the specified label and no keyboard shortcut. Note that use of "-" in a label is reserved to indicate a separator between menu items. By default, all menu items except for separators are enabled.

Parameters:
label - the label for this menu item.
Throws:
HeadlessException - if GraphicsEnvironment.isHeadless() returns true.

MenuItem

public MenuItem(String label,
                MenuShortcut s)
         throws HeadlessException
Create a menu item with an associated keyboard shortcut. Note that use of "-" in a label is reserved to indicate a separator between menu items. By default, all menu items except for separators are enabled.

Parameters:
label - the label for this menu item.
s - the instance of MenuShortcut associated with this menu item.
Throws:
HeadlessException - if GraphicsEnvironment.isHeadless() returns true.
Method Detail

addActionListener

public synchronized void addActionListener(ActionListener l)
Adds the specified action listener to receive action events from this menu item. If l is null, no exception is thrown and no action is performed.

Refer to AWT Threading Issues for details on AWT's threading model.

Parameters:
l - the action listener.

addNotify

public void addNotify()
Creates the menu item's peer. The peer allows us to modify the appearance of the menu item without changing its functionality.


deleteShortcut

public void deleteShortcut()
Delete any MenuShortcut object associated with this menu item.


disable

public synchronized void disable()

disableEvents

protected final void disableEvents(long eventsToDisable)
Disables event delivery to this menu item for events defined by the specified event mask parameter.

Parameters:
eventsToDisable - the event mask defining the event types

enable

public synchronized void enable()

enable

public void enable(boolean b)
Parameters:
b

enableEvents

protected final void enableEvents(long eventsToEnable)
Enables event delivery to this menu item for events to be defined by the specified event mask parameter

Since event types are automatically enabled when a listener for that type is added to the menu item, this method only needs to be invoked by subclasses of MenuItem which desire to have the specified event types delivered to processEvent regardless of whether a listener is registered.

Parameters:
eventsToEnable - the event mask defining the event types

getAccessibleContext

public AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this MenuItem. For menu items, the AccessibleContext takes the form of an AccessibleAWTMenuItem. A new AccessibleAWTMenuItem instance is created if necessary.

Overrides:
getAccessibleContext in class MenuComponent
Returns:
an AccessibleAWTMenuItem that serves as the AccessibleContext of this MenuItem

getActionCommand

public String getActionCommand()
Gets the command name of the action event that is fired by this menu item.


getActionListeners

public synchronized ActionListener[] getActionListeners()
Returns an array of all the action listeners registered on this menu item.

Returns:
all of this menu item's ActionListeners or an empty array if no action listeners are currently registered

getLabel

public String getLabel()
Gets the label for this menu item.

Returns:
the label of this menu item, or null if this menu item has no label.

getListeners

public EventListener[] getListeners(Class listenerType)
Returns an array of all the objects currently registered as FooListeners upon this MenuItem. FooListeners are registered using the addFooListener method.

You can specify the listenerType argument with a class literal, such as FooListener.class. For example, you can query a MenuItem m for its action listeners with the following code:

ActionListener[] als = (ActionListener[])(m.getListeners(ActionListener.class));
If no such listeners exist, this method returns an empty array.

Parameters:
listenerType - the type of listeners requested; this parameter should specify an interface that descends from java.util.EventListener
Returns:
an array of all objects registered as FooListeners on this menu item, or an empty array if no such listeners have been added

getShortcut

public MenuShortcut getShortcut()
Get the MenuShortcut object associated with this menu item,

Returns:
the menu shortcut associated with this menu item, or null if none has been specified.

isEnabled

public boolean isEnabled()
Checks whether this menu item is enabled.


paramString

public String paramString()
Returns a string representing the state of this MenuItem. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be null.

Overrides:
paramString in class MenuComponent
Returns:
the parameter string of this menu item

processActionEvent

protected void processActionEvent(ActionEvent e)
Processes action events occurring on this menu item, by dispatching them to any registered ActionListener objects. This method is not called unless action events are enabled for this component. Action events are enabled when one of the following occurs:

Note that if the event parameter is null the behavior is unspecified and may result in an exception.

Parameters:
e - the action event

processEvent

protected void processEvent(AWTEvent e)
Processes events on this menu item. If the event is an instance of ActionEvent, it invokes processActionEvent, another method defined by MenuItem.

Currently, menu items only support action events.

Note that if the event parameter is null the behavior is unspecified and may result in an exception.

Overrides:
processEvent in class MenuComponent
Parameters:
e - the event

removeActionListener

public synchronized void removeActionListener(ActionListener l)
Removes the specified action listener so it no longer receives action events from this menu item. If l is null, no exception is thrown and no action is performed.

Refer to AWT Threading Issues for details on AWT's threading model.

Parameters:
l - the action listener.

setActionCommand

public void setActionCommand(String command)
Sets the command name of the action event that is fired by this menu item.

By default, the action command is set to the label of the menu item.

Parameters:
command - the action command to be set for this menu item.

setEnabled

public synchronized void setEnabled(boolean b)
Sets whether or not this menu item can be chosen.

Parameters:
b - if true, enables this menu item; if false, disables it.

setLabel

public synchronized void setLabel(String label)
Sets the label for this menu item to the specified label.

Parameters:
label - the new label, or null for no label.

setShortcut

public void setShortcut(MenuShortcut s)
Set the MenuShortcut object associated with this menu item. If a menu shortcut is already associated with this menu item, it is replaced.

Parameters:
s - the menu shortcut to associate with this menu item.


This documentation differs from the official API. Jadeite adds extra features to the API including: variable font sizes, constructions examples, placeholders for classes and methods, and auto-generated “See Also” links. Additionally it is missing some items found in standard Javadoc documentation, including: generics type information, “Deprecated” tags and comments, “See Also” links, along with other minor differences. Please send any questions or feedback to bam@cs.cmu.edu.
This page displays the Jadeite version of the documention, which is derived from the offical documentation that contains this copyright notice:
Copyright 2008 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
The official Sun™ documentation can be found here at http://java.sun.com/javase/6/docs/api/.