[Chapter 19] Menu

#

| Chapter 19 java.awt Reference |

# Menu

## Name

Menu

##

## Description

The `Menu` class represents a group of `MenuItem` objects. `Menu`s themselves are menu items, allowing you to build multi-level menus. `Menus` are always attached to `MenuBar`s, which currently can only belong to frames.

## Class Definition

    public class java.awt.Menu
        extends java.awt.MenuItem
        implements java.awt.MenuContainer {

      // Constructors
      public Menu();
      public Menu (String label);
      public Menu (String label, boolean tearOff);

      // Instance Methods
      public synchronized MenuItem add (MenuItem item);
      public void add (String label);
      public void addNotify();
      public void addSeparator();
      public int countItems();
      public MenuItem getItem (int index);
      public int getItemCount();
      public void insert (String label, int index);
      public synchronized void insert (MenuItem menuitem, int index);
      public void insertSeparator (int index);
      public boolean isTearOff();
      public String paramString();
      public synchronized void remove (int index);
      public synchronized void remove (MenuComponent component);
      public synchronized void removeAll();
      public void removeNotify();
    }

## Constructors

### Menu

#### public Menu()

Description

  Constructs a `Menu` object.

#### public Menu (String label)

Parameters

  label

    Text that appears on `Menu`.

Description

  Constructs a `Menu` object with the given label.

#### public Menu (String label, boolean tearOff)

Parameters

  label

    Text that appears on `Menu`.

  tearOff

    `true` to create a tear-off menu, `false` otherwise.

Description

  Constructs a `Menu` object; this will be a tear-off menu if `tearOff` is set to `true`.

## Instance Methods

### add

#### public synchronized MenuItem add (MenuItem item)

Parameters

  item

    A `MenuItem` to add to the `Menu`.

Returns

  Item just added.

Description

  Adds a new item to a `Menu`.

#### public void add (String label)

Parameters

  label

    Text for a `MenuItem`

Description

  Constructs a new `MenuItem` object with the given label, and adds it to a `Menu`.

### addNotify

#### public void addNotify()

Overrides

  `MenuItem.addNotify()`

Description

  Creates a `Menu` peer, and peers for all `MenuItem` objects that appear on it.

### addSeparator

#### public void addSeparator()

Description

  Adds a separator bar to the `Menu`.

### countItems

#### public int countItems()

Returns

  The number of items on the menu. Replaced by `getItemCount()`.

### getItem

#### public MenuItem getItem (int index)

Parameters

  index

    The position of the `MenuItem` to fetch; the first item has index 0.

Returns

  The `MenuItem` at the designated position.

### getItemCount

#### public int getItemCount()

Returns

  The number of items on the menu.

### insert

#### public void insert (String label, int index)

Parameters

  label

    The label for the new item.

  index

    The position for the new item.

Description

  Adds a new item to this menu.

#### public synchronized void insert (MenuItem menuitem, int index)

Parameters

  menuitem

    The item to add.

  index

    The position for the new item.

Throws

  IllegalArgumentException

    If `index` is less than zero.

Description

  Adds a new item to this menu.

### insertSeparator

#### public void insertSeparator (int index)

Parameters

  index

    The position for the separator.

Throws

  IllegalArgumentException

    If `index` is less than zero.

Description

  Adds a separator to this menu.

### isTearOff

#### public boolean isTearOff()

Returns

  `true` if the menu is a tear-off menu, `false` otherwise.

### paramString

#### public String paramString()

Returns

  String with current settings of `Menu`.

Overrides

  `MenuItem.paramString()`

Description

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

### remove

#### public synchronized void remove (int index)

Parameters

  index

    The position of the `MenuItem` to remove.

Description

  Removes an item from the `Menu`.

#### public synchronized void remove (MenuComponent component)

Parameters

  component

    The element to remove.

Implements

  `MenuContainer.remove()`

Description

  Removes an item from the Menu.

### removeAll

#### public synchronized void removeAll()

Description

  Removes all items from the `Menu`.

### removeNotify

#### public void removeNotify()

Description

  Destroys `Menu` peer, and peers for all `MenuItem` objects that appear on it.

## See Also

`Frame`, `MenuComponent`, `MenuContainer`, `MenuItem`, `String`

MediaTracker |  | MenuBar