- All Implemented Interfaces:
- Serializable,- Accessible
- Direct Known Subclasses:
- CheckboxMenuItem,- Menu
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 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.
- See Also:
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionprotected classInner class of MenuItem used to provide default support for accessibility.Nested classes/interfaces declared in class java.awt.MenuComponentMenuComponent.AccessibleAWTMenuComponent
- 
Constructor SummaryConstructorsConstructorDescriptionMenuItem()Constructs a new MenuItem with an empty label and no keyboard shortcut.Constructs a new MenuItem with the specified label and no keyboard shortcut.MenuItem(String label, MenuShortcut s) Create a menu item with an associated keyboard shortcut.
- 
Method SummaryModifier and TypeMethodDescriptionvoidAdds the specified action listener to receive action events from this menu item.voidCreates the menu item's peer.voidDelete anyMenuShortcutobject associated with this menu item.voiddisable()Deprecated.protected final voiddisableEvents(long eventsToDisable) Disables event delivery to this menu item for events defined by the specified event mask parameter.voidenable()Deprecated.As of JDK version 1.1, replaced bysetEnabled(boolean).voidenable(boolean b) Deprecated.As of JDK version 1.1, replaced bysetEnabled(boolean).protected final voidenableEvents(long eventsToEnable) Enables event delivery to this menu item for events to be defined by the specified event mask parameterGets the AccessibleContext associated with this MenuItem.Gets the command name of the action event that is fired by this menu item.Returns an array of all the action listeners registered on this menu item.getLabel()Gets the label for this menu item.<T extends EventListener>
 T[]getListeners(Class<T> listenerType) Returns an array of all the objects currently registered asFooListeners upon thisMenuItem.Get theMenuShortcutobject associated with this menu item,booleanChecks whether this menu item is enabled.Returns a string representing the state of thisMenuItem.protected voidProcesses action events occurring on this menu item, by dispatching them to any registeredActionListenerobjects.protected voidProcesses events on this menu item.voidRemoves the specified action listener so it no longer receives action events from this menu item.voidsetActionCommand(String command) Sets the command name of the action event that is fired by this menu item.voidsetEnabled(boolean b) Sets whether or not this menu item can be chosen.voidSets the label for this menu item to the specified label.voidSet theMenuShortcutobject associated with this menu item.Methods declared in class java.awt.MenuComponentdispatchEvent, getFont, getName, getParent, getTreeLock, postEvent, removeNotify, setFont, setName, toString
- 
Constructor Details- 
MenuItemConstructs a new MenuItem with an empty label and no keyboard shortcut.- Throws:
- HeadlessException- if GraphicsEnvironment.isHeadless() returns true.
- Since:
- 1.1
- See Also:
 
- 
MenuItemConstructs 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.
- Since:
- 1.0
- See Also:
 
- 
MenuItemCreate 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- MenuShortcutassociated with this menu item.
- Throws:
- HeadlessException- if GraphicsEnvironment.isHeadless() returns true.
- Since:
- 1.1
- See Also:
 
 
- 
- 
Method Details- 
addNotifypublic void addNotify()Creates the menu item's peer. The peer allows us to modify the appearance of the menu item without changing its functionality.
- 
getLabelGets the label for this menu item.- Returns:
- the label of this menu item, or nullif this menu item has no label.
- Since:
- 1.0
- See Also:
 
- 
setLabelSets the label for this menu item to the specified label.- Parameters:
- label- the new label, or- nullfor no label.
- Since:
- 1.0
- See Also:
 
- 
isEnabledpublic boolean isEnabled()Checks whether this menu item is enabled.- Returns:
- trueif the item is enabled; otherwise- false
- Since:
- 1.0
- See Also:
 
- 
setEnabledpublic 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.
- Since:
- 1.1
- See Also:
 
- 
enableDeprecated.As of JDK version 1.1, replaced bysetEnabled(boolean).
- 
enableDeprecated.As of JDK version 1.1, replaced bysetEnabled(boolean).Sets whether or not this menu item can be chosen.- Parameters:
- b- if- true, enables this menu item; otherwise disables
 
- 
disableDeprecated.As of JDK version 1.1, replaced bysetEnabled(boolean).
- 
getShortcutGet theMenuShortcutobject associated with this menu item,- Returns:
- the menu shortcut associated with this menu item,
                   or nullif none has been specified.
- Since:
- 1.1
- See Also:
 
- 
setShortcutSet theMenuShortcutobject 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.
- Since:
- 1.1
- See Also:
 
- 
deleteShortcutpublic void deleteShortcut()Delete anyMenuShortcutobject associated with this menu item.- Since:
- 1.1
 
- 
enableEventsprotected final void enableEvents(long eventsToEnable) Enables event delivery to this menu item for events to be defined by the specified event mask parameterSince 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 MenuItemwhich desire to have the specified event types delivered toprocessEventregardless of whether a listener is registered.- Parameters:
- eventsToEnable- the event mask defining the event types
- Since:
- 1.1
- See Also:
 
- 
disableEventsprotected 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
- Since:
- 1.1
- See Also:
 
- 
setActionCommandSets 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.
- Since:
- 1.1
- See Also:
 
- 
getActionCommandGets the command name of the action event that is fired by this menu item.- Returns:
- the action command name
- Since:
- 1.1
- See Also:
 
- 
addActionListenerAdds 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.
- Since:
- 1.1
- See Also:
 
- 
removeActionListenerRemoves 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.
- Since:
- 1.1
- See Also:
 
- 
getActionListenersReturns 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
- Since:
- 1.4
- See Also:
 
- 
getListenersReturns an array of all the objects currently registered asFooListeners upon thisMenuItem.FooListeners are registered using theaddFooListenermethod.You can specify the listenerTypeargument with a class literal, such asFooListener.class. For example, you can query aMenuItem mfor 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.- Type Parameters:
- T- the type of the listeners
- 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
- Throws:
- ClassCastException- if- listenerTypedoesn't specify a class or interface that implements- java.util.EventListener
- Since:
- 1.3
- See Also:
 
- 
processEventProcesses events on this menu item. If the event is an instance ofActionEvent, it invokesprocessActionEvent, another method defined byMenuItem.Currently, menu items only support action events. Note that if the event parameter is nullthe behavior is unspecified and may result in an exception.- Overrides:
- processEventin class- MenuComponent
- Parameters:
- e- the event
- Since:
- 1.1
- See Also:
 
- 
processActionEventProcesses action events occurring on this menu item, by dispatching them to any registeredActionListenerobjects. This method is not called unless action events are enabled for this component. Action events are enabled when one of the following occurs:- An ActionListenerobject is registered viaaddActionListener.
- Action events are enabled via enableEvents.
 Note that if the event parameter is nullthe behavior is unspecified and may result in an exception.- Parameters:
- e- the action event
- Since:
- 1.1
- See Also:
 
- An 
- 
paramStringReturns a string representing the state of thisMenuItem. 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 benull.- Overrides:
- paramStringin class- MenuComponent
- Returns:
- the parameter string of this menu item
 
- 
getAccessibleContextGets 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.- Specified by:
- getAccessibleContextin interface- Accessible
- Overrides:
- getAccessibleContextin class- MenuComponent
- Returns:
- an AccessibleAWTMenuItem that serves as the AccessibleContext of this MenuItem
- Since:
- 1.3
 
 
- 
setEnabled(boolean).