[Chapter 19] MenuItem

#

| Chapter 19 java.awt Reference |

# MenuItem

## Name

MenuItem

##

## Description

The `MenuItem` class represents a selectable item on a menu.

## Class Definition

    public class java.awt.MenuItem
        extends java.awt.MenuComponent {

      // Constructors
      public MenuItem();
      public MenuItem (String label);
      public MenuItem (String label, MenuShortcut s);

      // Instance Methods
      public void addActionListener (ActionListener l);
      public void addNotify();
      public void deleteShortcut();
      public synchronized void disable();
      public synchronized void enable();
      public void enable (boolean condition);
      public String getActionCommand();
      public String getLabel();
      public MenuShortcut getShortcut();
      public boolean isEnabled();
      public String paramString();
      public void removeActionListener (ActionListener l);
      public void setActionCommand (String command);
      public synchronized void setEnabled (boolean b);
      public synchronized void setLabel (String label);
      public void setShortcut (MenuShortcut s);
      // Protected Instance Methods
      protected final void disableEvents (long eventsToDisable);
      protected final void enableEvents (long eventsToEnable);
      protected void processActionEvent (ActionEvent e);
      protected void processEvent (AWTEvent e);
    }

## Constructors

### MenuItem

#### public MenuItem()

Description

  Constructs a `MenuItem` object with no label or shortcut.

#### public MenuItem (String label)

Parameters

  label

    Text that appears on the `MenuItem`.

Description

  Constructs a `MenuItem` object.

#### public MenuItem (String label, MenuShortcut s)

Parameters

  label

    Text that appears on the `MenuItem`.

  s

    Shortcut for the `MenuItem`.

Description

  Constructs a `MenuItem` object with the given shortcut.

## Instance Methods

### addActionListener

#### public void addActionListener(ActionListener l)

Parameters

  l

    An object that implements the `ActionListener` interface.

Description

  Add a listener for the action event.

### addNotify

#### public void addNotify()

Description

  Creates the `MenuItem`'s peer.

### deleteShortcut

#### public void deleteShortcut()

Description

  Removes the shortcut associated with this item.

### disable

#### public synchronized void disable()

Description

  Disables the menu component so that it is unresponsive to user interactions. Replaced by `setEnabled(false)`.

### enable

#### public synchronized void enable()

Description

  Enables the menu component so that it is responsive to user interactions. Replaced by `setEnabled(true)`.

#### public void enable (boolean condition)

Parameters

  condition

    `true` to enable the menu component; `false` to disable it.

Description

  Enables or disables the menu component, depending on the `condition` parameter. Replaced by `setEnabled(boolean)`.

### getActionCommand

#### public String getActionCommand()

Returns

  Current action command string.

Description

  Returns the string used for the action command.

### getLabel

#### public String getLabel()

Returns

  The current text associated with the `MenuItem`.

### getShortcut

#### public MenuShortcut getShortcut()

Returns

  The current shortcut for this item, or `null` if there is none.

### isEnabled

#### public boolean isEnabled()

Returns

  `true` if the menu item is enabled, `false` otherwise.

### paramString

#### public String paramString()

Returns

  String with current settings of `MenuItem`.

Description

  Helper method for `toString()` to generate string of current settings.

### removeActionListener

#### public void removeActionListener(ActionListener l)

Parameters

  l

    One of this `Button`'s `ActionListener`s.

Description

  Remove an action event listener.

### setActionCommand

#### public void setActionCommand(String command)

Parameters

  command

    New action command string.

Description

  Specify the string used for the action command.

### setEnabled

#### public synchronized void setEnabled (boolean b)

Parameters

  b

    `true` to enable the item, `false` to disable it.

Description

  Enables or disables the item. Replaces `enable()`, `enable(boolean)`, and `disable()`.

### setLabel

#### public synchronized void setLabel (String label)

Parameters

  label

    New text to appear on `MenuItem`.

Description

  Changes the label of the `MenuItem`.

### setShortcut

#### public void setShortcut (MenuShortcut s)

Parameters

  s

    New shortcut for the `MenuItem`.

Description

  Changes the shortcut of the `MenuItem`.

## Protected Instance Methods

### disableEvents

#### protected final void disableEvents (long eventsToDisable)

Parameters

  eventsToDisable

    A value representing certain kinds of events. This can be constructed by ORing the event mask constants defined in `java.awt.AWTEvent`.

Description

  By default, a menu item receives events corresponding to the event listeners that have registered. If a menu item should not receive events of a certain type, even if there is a listener registered for that type of event, this method can be used to disable that event type.

### enableEvents

#### protected final void enableEvents (long eventsToEnable)

Parameters

  eventsToDisable

    A value representing certain kinds of events. This can be constructed by ORing the event mask constants defined in `java.awt.AWTEvent`.

Description

  By default, a menu item receives events corresponding to the event listeners that have registered. If a menu item should receive other types of events as well, this method can be used to get them.

### processActionEvent

#### protected void processActionEvent (ActionEvent e)

Parameters

  e

    The action event to process.

Description

  Action events are passed to this method for processing. Normally, this method is called by `processEvent()`.

### processEvent

#### protected void processEvent (AWTEvent e)

Parameters

  e

    The event to process.

Description

  Low-level `AWTEvent`s are passed to this method for processing.

## See Also

`CheckboxMenuItem`, `Menu`, `MenuComponent`, `MenuShortcut`, `String`

MenuContainer |  | MenuShortcut