java.awt
Class MenuItem

java.lang.Object
  |
  +--java.awt.MenuComponent
        |
        +--java.awt.MenuItem
Direct Known Subclasses:
CheckboxMenuItem, Menu

public class MenuItem
extends MenuComponent

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 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:
Serialized Form

Field Summary
(package private)  String actionCommand
          This field indicates the command tha has been issued by a particular menu item.
(package private)  ActionListener actionListener
           
private static String base
           
(package private)  boolean enabled
          A value to indicate whether a menu item is enabled or not.
(package private)  long eventMask
          The eventMask is ONLY set by subclasses via enableEvents.
(package private)  String label
          label is the label of a menu item.
private  int menuItemSerializedDataVersion
          Menu item serialized data version.
private static int nameCounter
           
private static long serialVersionUID
           
private  MenuShortcut shortcut
          A sequence of key stokes that ia associated with a menu item.
 
Fields inherited from class java.awt.MenuComponent
actionListenerK, appContext, font, itemListenerK, name, nameExplicitlySet, newEventsOnly, parent, peer, serialVersionUID
 
Constructor Summary
MenuItem()
          Constructs a new MenuItem with an empty label and no keyboard shortcut.
MenuItem(String label)
          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 Summary
(package private) static void ()
           
 void addActionListener(ActionListener l)
          Adds the specified action listener to receive action events from this menu item.
 void addNotify()
          Creates the menu item's peer.
(package private)  String constructComponentName()
          Construct a name for this MenuComponent.
 void deleteShortcut()
          Delete any MenuShortcut object associated with this menu item.
(package private)  void deleteShortcut(MenuShortcut s)
           
 void disable()
          Deprecated. As of JDK version 1.1, replaced by setEnabled(boolean).
protected  void disableEvents(long eventsToDisable)
          Disables event delivery to this menu item for events defined by the specified event mask parameter.
 void enable()
          Deprecated. As of JDK version 1.1, replaced by setEnabled(boolean).
 void enable(boolean b)
          Deprecated. As of JDK version 1.1, replaced by setEnabled(boolean).
protected  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.
(package private)  boolean eventEnabled(AWTEvent e)
           
 String getActionCommand()
          Gets the command name of the action event that is fired by this menu item.
 String getLabel()
          Gets the label for this menu item.
 MenuShortcut getShortcut()
          Get the MenuShortcut object associated with this menu item,
(package private)  MenuItem getShortcutMenuItem(MenuShortcut s)
           
(package private)  boolean handleShortcut(KeyEvent e)
           
private static void initIDs()
          Initialize JNI field and method IDs
 boolean isEnabled()
          Checks whether this menu item is enabled.
 String paramString()
          Returns the parameter string representing the state of this menu item.
protected  void processActionEvent(ActionEvent e)
          Processes action events occurring on this menu item, by dispatching them to any registered ActionListener objects.
protected  void processEvent(AWTEvent e)
          Processes events on this menu item.
private  void readObject(ObjectInputStream s)
          Read the ObjectInputStream and if it isnt null add a listener to receive item events fired by the Menu Item.
 void removeActionListener(ActionListener l)
          Removes the specified action listener so it no longer receives action events from this menu item.
 void setActionCommand(String command)
          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 setLabel(String label)
          Sets the label for this menu item to the specified label.
 void setShortcut(MenuShortcut s)
          Set the MenuShortcut object associated with this menu item.
private  void writeObject(ObjectOutputStream s)
          Writes default serializable fields to stream.
 
Methods inherited from class java.awt.MenuComponent
dispatchEvent, dispatchEventImpl, getFont_NoClientCode, getFont, getName, getParent_NoClientCode, getParent, getPeer, getTreeLock, postEvent, removeNotify, setFont, setName, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, wait, wait, wait
 

Field Detail

enabled

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.
See Also:
isEnabled(), setEnabled()

label

String label
label is the label of a menu item. It can be any string.
See Also:
getLabel(), setLabel()

actionCommand

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.
See Also:
setActionCommand(), getActionCommand()

eventMask

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.

actionListener

transient ActionListener actionListener

shortcut

private MenuShortcut shortcut
A sequence of key stokes that ia associated with a menu item. Note :in 1.1.2 you must use setActionCommand() on a menu item in order for its shortcut to work.
See Also:
getShortcut(), setShortcut(), deleteShortcut()

base

private static final String base

nameCounter

private static int nameCounter

serialVersionUID

private static final long serialVersionUID

menuItemSerializedDataVersion

private int menuItemSerializedDataVersion
Menu item serialized data version.
Constructor Detail

MenuItem

public MenuItem()
Constructs a new MenuItem with an empty label and no keyboard shortcut.
Since:
JDK1.1

MenuItem

public MenuItem(String label)
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.
Since:
JDK1.0

MenuItem

public MenuItem(String label,
                MenuShortcut s)
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.
Since:
JDK1.1
Method Detail

static void ()

constructComponentName

String constructComponentName()
Construct a name for this MenuComponent. Called by getName() when the name is null.
Overrides:
constructComponentName in class MenuComponent

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.

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.
Since:
JDK1.0
See Also:
setLabel(java.lang.String)

setLabel

public 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.
Since:
JDK1.0
See Also:
getLabel()

isEnabled

public boolean isEnabled()
Checks whether this menu item is enabled.
Since:
JDK1.0
See Also:
setEnabled(boolean)

setEnabled

public 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:
JDK1.1
See Also:
isEnabled()

enable

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

enable

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

disable

public void disable()
Deprecated. As of JDK version 1.1, replaced by setEnabled(boolean).

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.
Since:
JDK1.1
See Also:
setShortcut(java.awt.MenuShortcut)

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.
Since:
JDK1.1
See Also:
getShortcut()

deleteShortcut

public void deleteShortcut()
Delete any MenuShortcut object associated with this menu item.
Since:
JDK1.1

deleteShortcut

void deleteShortcut(MenuShortcut s)

handleShortcut

boolean handleShortcut(KeyEvent e)

getShortcutMenuItem

MenuItem getShortcutMenuItem(MenuShortcut s)

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.
Since:
JDK1.1
See Also:
processEvent(java.awt.AWTEvent), disableEvents(long), Component.enableEvents(long)

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.
Since:
JDK1.1
See Also:
processEvent(java.awt.AWTEvent), enableEvents(long), Component.disableEvents(long)

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.
Since:
JDK1.1
See Also:
getActionCommand()

getActionCommand

public String getActionCommand()
Gets the command name of the action event that is fired by this menu item.
Since:
JDK1.1
See Also:
setActionCommand(java.lang.String)

addActionListener

public 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.
Parameters:
l - the action listener.
Since:
JDK1.1
See Also:
ActionEvent, ActionListener, removeActionListener(java.awt.event.ActionListener)

removeActionListener

public 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.
Parameters:
l - the action listener.
Since:
JDK1.1
See Also:
ActionEvent, ActionListener, addActionListener(java.awt.event.ActionListener)

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.

Parameters:
e - the event.
Overrides:
processEvent in class MenuComponent
Since:
JDK1.1
See Also:
processActionEvent(java.awt.event.ActionEvent)

eventEnabled

boolean eventEnabled(AWTEvent e)
Overrides:
eventEnabled in class MenuComponent

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:

Parameters:
e - the action event.
Since:
JDK1.1
See Also:
ActionEvent, ActionListener, enableEvents(long)

paramString

public String paramString()
Returns the parameter string representing the state of this menu item. This string is useful for debugging.
Returns:
the parameter string of this menu item.
Overrides:
paramString in class MenuComponent
Since:
JDK1.0

writeObject

private void writeObject(ObjectOutputStream s)
                  throws IOException
Writes default serializable fields to stream. Writes a list of serializable ItemListener(s) as optional data. The non-serializable ItemListner(s) are detected and no attempt is made to serialize them.
See Also:
AWTEventMulticaster.save(ObjectOutputStream, String, EventListener), java.awt.Component.itemListenerK

readObject

private void readObject(ObjectInputStream s)
                 throws ClassNotFoundException,
                        IOException
Read the ObjectInputStream and if it isnt null add a listener to receive item events fired by the Menu Item. Unrecognised keys or values will be Ignored.
Overrides:
readObject in class MenuComponent
See Also:
removeActionListener(), addActionListener()

initIDs

private static void initIDs()
Initialize JNI field and method IDs