AWT Menu Classes-AWT Menu Class - AWT (Abstract Window Toolkit)

What is the use of AWT Menu Class?

Introduction

Menu class represents pull-down menu component which is deployed from a menu bar.

Class declaration

Below is the declaration for java.awt.Menu class:

Class constructors

S.N.

Constructor & Description

1

Menu()

Constructs a new menu with an empty label.

2

Menu(String label)

Constructs a new menu with the specified label.

3

Menu(String label, boolean tearOff)

Constructs a new menu with the specified label, indicating whether the menu can be torn off.

Class methods

S.N.

Method & Description

1

MenuItem add(MenuItem mi)

Adds the specified menu item to this menu.

2

void add(String label)

Adds an item with the specified label to this menu.

3

void addNotify()

Creates the menu's peer.

4

void addSeparator()

Adds a separator line, or a hypen, to the menu at the current position.

5

int countItems()

Deprecated. As of JDK version 1.1, replaced by getItemCount().

6

AccessibleContext getAccessibleContext()

Gets the AccessibleContext associated with this Menu.

7

MenuItem getItem(int index)

Gets the item located at the specified index of this menu.

8

int getItemCount()

Get the number of items in this menu.

9

void insert(MenuItem menuitem, int index)

Inserts a menu item into this menu at the specified position.

10

void insert(String label, int index)

Inserts a menu item with the specified label into this menu at the specified position.

11

void insertSeparator(int index)

Inserts a separator at the specified position.

12

boolean isTearOff()

Indicates whether this menu is a tear-off menu.

13

String paramString()

Returns a string representing the state of this Menu.

14

void remove(int index)

Removes the menu item at the specified index from this menu.

15

void remove(MenuComponent item)

Removes the specified menu item from this menu.

16

void removeAll()

Removes all items from this menu.

17

void removeNotify()

Removes the menu's peer.

Methods inherited

This class inherits methods from the following classes:

  • java.awt.MenuItem
  • java.awt.MenuComponent
  • java.lang.Object

Menu Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > wisdomjobs > gui >

AWTMenuDemo.java

Compile the program using command prompt. Go to D:/ > AWT and type the following command.

If no error comes that means compilation is successful. Run the program using following command.

Verify the following output. (Click on File Menu.)

awt_menu

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

AWT (Abstract Window Toolkit) Topics