Save This Page
Home » openjdk-7 » java » awt » [javadoc | source]
java.awt
public class: MenuItem [javadoc | source]
java.lang.Object
   java.awt.MenuComponent
      java.awt.MenuItem

All Implemented Interfaces:
    Accessible, Serializable

Direct Known Subclasses:
    PopupMenu, Menu, CheckboxMenuItem

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  MenuItem.AccessibleAWTMenuItem  Inner class of MenuItem used to provide default support for accessibility. This class is not meant to be used directly by application developers, but is instead meant only to be subclassed by menu component developers.

This class implements accessibility support for the MenuItem class. It provides an implementation of the Java Accessibility API appropriate to menu item user-interface elements. 

Field Summary
 boolean enabled    A value to indicate whether a menu item is enabled or not. If it is enabled, enabled will be set to true. Else enabled will be set to false. 
 String label    label is the label of a menu item. It can be any string. 
 String actionCommand    This field indicates the command tha has been issued by a particular menu item. By default the actionCommand is the label of the menu item, unless it has been set using setActionCommand. 
 long eventMask    The eventMask is ONLY set by subclasses via enableEvents. The mask should NOT be set when listeners are registered so that we can distinguish the difference between when listeners request events and subclasses request them.
    serial:
 
transient  ActionListener actionListener     
Fields inherited from java.awt.MenuComponent:
peer,  parent,  appContext,  font,  newEventsOnly,  actionListenerK,  itemListenerK,  accessibleContext
Constructor:
 public MenuItem() throws HeadlessException 
    Constructs a new MenuItem with an empty label and no keyboard shortcut.
    Throws:
    HeadlessException - if GraphicsEnvironment.isHeadless() returns true.
    Also see:
    java.awt.GraphicsEnvironment#isHeadless
    exception: HeadlessException - if GraphicsEnvironment.isHeadless() returns true.
    since: JDK1.1 -
 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.
    Also see:
    java.awt.GraphicsEnvironment#isHeadless
    exception: HeadlessException - if GraphicsEnvironment.isHeadless() returns true.
    since: JDK1.0 -
 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.
    Also see:
    java.awt.GraphicsEnvironment#isHeadless
    exception: HeadlessException - if GraphicsEnvironment.isHeadless() returns true.
    since: JDK1.1 -
Method from java.awt.MenuItem Summary:
addActionListener,   addNotify,   constructComponentName,   deleteShortcut,   deleteShortcut,   disable,   disableEvents,   doMenuEvent,   enable,   enable,   enableEvents,   eventEnabled,   getAccessibleContext,   getActionCommand,   getActionCommandImpl,   getActionListeners,   getLabel,   getListeners,   getShortcut,   getShortcutMenuItem,   handleShortcut,   isEnabled,   paramString,   processActionEvent,   processEvent,   removeActionListener,   setActionCommand,   setEnabled,   setLabel,   setShortcut
Methods from java.awt.MenuComponent:
constructComponentName,   dispatchEvent,   dispatchEventImpl,   eventEnabled,   getAccessControlContext,   getAccessibleChildIndex,   getAccessibleContext,   getAccessibleIndexInParent,   getAccessibleStateSet,   getFont,   getFont_NoClientCode,   getName,   getParent,   getParent_NoClientCode,   getPeer,   getTreeLock,   paramString,   postEvent,   processEvent,   removeNotify,   setFont,   setName,   toString
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from java.awt.MenuItem Detail:
 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.

 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.
 String constructComponentName() 
    Construct a name for this MenuComponent. Called by getName() when the name is null.
 public  void deleteShortcut() 
    Delete any MenuShortcut object associated with this menu item.
  void deleteShortcut(MenuShortcut s) 
 public synchronized  void disable() 
Deprecated! As - of JDK version 1.1, replaced by setEnabled(boolean).

 protected final  void disableEvents(long eventsToDisable) 
    Disables event delivery to this menu item for events defined by the specified event mask parameter.
  void doMenuEvent(long when,
    int modifiers) 
 public synchronized  void enable() 
Deprecated! As - of JDK version 1.1, replaced by setEnabled(boolean).

 public  void enable(boolean b) 
Deprecated! As - of JDK version 1.1, replaced by setEnabled(boolean).

 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.

 boolean eventEnabled(AWTEvent e) 
 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.
 public String getActionCommand() 
    Gets the command name of the action event that is fired by this menu item.
 final String getActionCommandImpl() 
 public synchronized ActionListener[] getActionListeners() 
    Returns an array of all the action listeners registered on this menu item.
 public String getLabel() 
    Gets the label for this menu item.
 public T[] getListeners(Class<T> 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.
 public MenuShortcut getShortcut() 
    Get the MenuShortcut object associated with this menu item,
 MenuItem getShortcutMenuItem(MenuShortcut s) 
 boolean handleShortcut(KeyEvent e) 
 public boolean isEnabled() 
    Checks whether this menu item is enabled.
 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.
 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:

    • An ActionListener object is registered via addActionListener.
    • Action events are enabled via enableEvents.

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

 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.

 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.

 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.

 public synchronized  void setEnabled(boolean b) 
    Sets whether or not this menu item can be chosen.
 public synchronized  void setLabel(String label) 
    Sets the label for this menu item to the specified label.
 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.