Class Menu in AWT in Java

0
161
Spread the love
  • 29
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    138
    Shares

Class Menu in AWT in Java

A menu is a pull-down component of a Menu bar. A menu can optionally be a tear-off menu. A tear-off menu can remain on the screen after the mouse button has been released. The mechanism for tearing off a menu is platform dependent.

Each Menu must have a label that appears in the MenuBar. Menu belongs to the MenuItem class. This can be an instance of MenuItem, a sub-menu(an instance of Menu) or a checkbox(an instance of CheckboxMenuItem).

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

The class structure of Menu class is given as:

public class java.awt.Menu extends java.awt.MenuItem implements java.awt.MenuContainer{
//constructor
public Menu();
public Menu(String label);
public Menu(String label,boolean tearoff);
//methods:
public synchronized MenuItem add(MenuItem menu);
public void add(String label);
public void addNotify();
public void addSeparator();
public int countItems();
public int getItemCount();
public synchronized void insert(MenuItem menuItem,int index);
public void insert(String label,int index);
public void insertSeparator(int index);
public MenuItem getItem(int index);
public boolean isTearoff();
public String paramString();
public synchronized void remove(int index);
public synchronized void remove(MenuComponent item);
public synchronized void removeAll();
public void removeNotify();
} 

The details of the class structure are given as follows:

public Menu();

public Menu() constructs a Menu object.

public Menu(String label);

public Menu (String label) constructs a Menu object with the given label.

Parameter
label – Text that appears on Menu.

public Menu(String label, boolean tearOff);

public Menu (String label, boolean tearOff) constructs a Menu object; this will be a tear-off menu if tearOff is set to true.

Parameter
label – Text that appears on Menu.
tearOff -true to create a tear-off menu, false otherwise.

public synchronized MenuItem add(MenuItem item);

public synchronized MenuItem add (MenuItem item) method adds a new item to a Menu.

This method returns the item just added.

Parameter
item – A MenuItem to add to the Menu.

public void add(String label);

public void add(String label) constructs a new MenuItem object with the given label, and adds it to a Menu.

Parameter
label – Text for a MenuItem

public void addNotify();

public void addNotify() method creates a Menu peer, and peers for all MenuItem objects that appear on it.

public void addSeparator();

public void addSeparator() method adds a separator bar to the Menu.

public int countItems();

public int countItems() method returns the number of items on the menu. Replaced by getItemCount().

This method returns the number of items on the menu. Replaced by getItemCount().

public MenuItem getItem(int index);

public MenuItem getItem(int index) method returns the MenuItem at the designated position.

This method returns the MenuItem at the designated position.

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

public int getItemCount();

public int getItemCount() method returns the number of items on the menu.

This method returns the number of items on the menu.

public void insert(String label, int index);

public void insert(String label, int index) method adds a new item to this menu.

Parameter
label – The label for the new item.
index – The position for the new item.

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

public synchronized void insert(MenuItem menuitem, int index) method adds a new item to this menu.

Parameter
menuitem – The item to add.
index – The position for the new item.

public void insertSeparator(int index);

public void insertSeparator(int index) method adds a separator to this menu.

Parameter
index – The position for the separator.

public boolean isTearOff();

public boolean isTearOff() method returns true if the menu is a tear-off menu, false otherwise.

This method returns true if the menu is a tear-off menu, false otherwise.

public String paramString();

public String paramString() method is a helper method for toString() to generate string of current settings.

This method returns the String with the current settings of Menu.

public synchronized void remove(int index);

public synchronized void remove(int index) method removes an item from the Menu.

Parameter
index – The position of the MenuItem to remove.

public synchronized void remove(MenuComponent component);

public synchronized void remove (MenuComponent component) method removes an item from the Menu.

Parameter
component – The element to remove.

public synchronized void removeAll();

public synchronized void removeAll() method removes all items from the Menu.

public void removeNotify();

public void removeNotify() method destroys Menu peer, and peers for all MenuItem objects that appear on it.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 29
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    138
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here