FileDocCategorySizeDatePackage
Menu.javaAPI DocJava SE 6 API18013Tue Jun 10 00:25:16 BST 2008java.awt

Menu

public class Menu extends MenuItem implements MenuContainer, Accessible
A Menu object is a pull-down menu component that is deployed from a menu bar.

A menu can optionally be a tear-off menu. A tear-off menu can be opened and dragged away from its parent menu bar or menu. It remains on the screen after the mouse button has been released. The mechanism for tearing off a menu is platform dependent, since the look and feel of the tear-off menu is determined by its peer. On platforms that do not support tear-off menus, the tear-off property is ignored.

Each item in a menu must belong to the MenuItem class. It can be an instance of MenuItem, a submenu (an instance of Menu), or a check box (an instance of CheckboxMenuItem).

version
1.78, 04/07/06
author
Sami Shaio
see
java.awt.MenuItem
see
java.awt.CheckboxMenuItem
since
JDK1.0

Fields Summary
Vector
items
A vector of the items that will be part of the Menu.
boolean
tearOff
This field indicates whether the menu has the tear of property or not. It will be set to true if the menu has the tear off property and it will be set to false if it does not. A torn off menu can be deleted by a user when it is no longer needed.
boolean
isHelpMenu
This field will be set to true if the Menu in question is actually a help menu. Otherwise it will be set to false.
private static final String
base
private static int
nameCounter
private static final long
serialVersionUID
private int
menuSerializedDataVersion
The menu serialized Data Version.
Constructors Summary
public Menu()
Constructs a new menu with an empty label. This menu is not a tear-off menu.

exception
HeadlessException if GraphicsEnvironment.isHeadless() returns true.
see
java.awt.GraphicsEnvironment#isHeadless
since
JDK1.1


                                       
        
	this("", false);
    
public Menu(String label)
Constructs a new menu with the specified label. This menu is not a tear-off menu.

param
label the menu's label in the menu bar, or in another menu of which this menu is a submenu.
exception
HeadlessException if GraphicsEnvironment.isHeadless() returns true.
see
java.awt.GraphicsEnvironment#isHeadless

	this(label, false);
    
public Menu(String label, boolean tearOff)
Constructs a new menu with the specified label, indicating whether the menu can be torn off.

Tear-off functionality may not be supported by all implementations of AWT. If a particular implementation doesn't support tear-off menus, this value is silently ignored.

param
label the menu's label in the menu bar, or in another menu of which this menu is a submenu.
param
tearOff if true, the menu is a tear-off menu.
exception
HeadlessException if GraphicsEnvironment.isHeadless() returns true.
see
java.awt.GraphicsEnvironment#isHeadless
since
JDK1.0.

	super(label);
	this.tearOff = tearOff;
    
Methods Summary
public java.awt.MenuItemadd(java.awt.MenuItem mi)
Adds the specified menu item to this menu. If the menu item has been part of another menu, removes it from that menu.

param
mi the menu item to be added
return
the menu item added
see
java.awt.Menu#insert(java.lang.String, int)
see
java.awt.Menu#insert(java.awt.MenuItem, int)

        synchronized (getTreeLock()) {
	    if (mi.parent != null) {
	        mi.parent.remove(mi);
	    }
	    items.addElement(mi);
	    mi.parent = this;
	    MenuPeer peer = (MenuPeer)this.peer;
	    if (peer != null) {
	        mi.addNotify();
		peer.addItem(mi);
	    }
	    return mi;
	}
    
public voidadd(java.lang.String label)
Adds an item with the specified label to this menu.

param
label the text on the item
see
java.awt.Menu#insert(java.lang.String, int)
see
java.awt.Menu#insert(java.awt.MenuItem, int)

	add(new MenuItem(label));
    
public voidaddNotify()
Creates the menu's peer. The peer allows us to modify the appearance of the menu without changing its functionality.

        synchronized (getTreeLock()) {
	    if (peer == null)
	        peer = Toolkit.getDefaultToolkit().createMenu(this);
	    int nitems = getItemCount();
	    for (int i = 0 ; i < nitems ; i++) {
	        MenuItem mi = getItem(i);
		mi.parent = this;
		mi.addNotify();
	    }
	}
    
public voidaddSeparator()
Adds a separator line, or a hypen, to the menu at the current position.

see
java.awt.Menu#insertSeparator(int)

	add("-");
    
java.lang.StringconstructComponentName()
Construct a name for this MenuComponent. Called by getName() when the name is null.

        synchronized (getClass()) {
	    return base + nameCounter++;
	}
    
public intcountItems()

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

	return countItemsImpl();
    
final intcountItemsImpl()

	return items.size();
    
voiddeleteShortcut(java.awt.MenuShortcut s)

	int nitems = getItemCount();
	for (int i = 0 ; i < nitems ; i++) {
	    getItem(i).deleteShortcut(s);
	}
    
intgetAccessibleChildIndex(java.awt.MenuComponent child)
Defined in MenuComponent. Overridden here.

        return items.indexOf(child);
    
public javax.accessibility.AccessibleContextgetAccessibleContext()
Gets the AccessibleContext associated with this Menu. For menus, the AccessibleContext takes the form of an AccessibleAWTMenu. A new AccessibleAWTMenu instance is created if necessary.

return
an AccessibleAWTMenu that serves as the AccessibleContext of this Menu
since
1.3

        if (accessibleContext == null) {
            accessibleContext = new AccessibleAWTMenu();
        }
        return accessibleContext;
    
public java.awt.MenuItemgetItem(int index)
Gets the item located at the specified index of this menu.

param
index the position of the item to be returned.
return
the item located at the specified index.

	return getItemImpl(index);
    
public intgetItemCount()
Get the number of items in this menu.

return
the number of items in this menu.
since
JDK1.1

	return countItems();
    
final java.awt.MenuItemgetItemImpl(int index)

	return (MenuItem)items.elementAt(index);
    
java.awt.MenuItemgetShortcutMenuItem(java.awt.MenuShortcut s)

	int nitems = getItemCount();
	for (int i = 0 ; i < nitems ; i++) {
            MenuItem mi = getItem(i).getShortcutMenuItem(s);
            if (mi != null) {
                return mi;
            }
	}
        return null;
    
booleanhandleShortcut(java.awt.event.KeyEvent e)

        int nitems = getItemCount();
        for (int i = 0 ; i < nitems ; i++) {
            MenuItem mi = getItem(i);
            if (mi.handleShortcut(e)) {
                return true;
            }
        }
        return false;
    
private static native voidinitIDs()
Initialize JNI field and method IDs

public voidinsert(java.awt.MenuItem menuitem, int index)
Inserts a menu item into this menu at the specified position.

param
menuitem the menu item to be inserted.
param
index the position at which the menu item should be inserted.
see
java.awt.Menu#add(java.lang.String)
see
java.awt.Menu#add(java.awt.MenuItem)
exception
IllegalArgumentException if the value of index is less than zero
since
JDK1.1

        synchronized (getTreeLock()) {
	    if (index < 0) {
	        throw new IllegalArgumentException("index less than zero.");
	    }

	    int nitems = getItemCount();
	    Vector tempItems = new Vector();

	    /* Remove the item at index, nitems-index times 
	       storing them in a temporary vector in the
	       order they appear on the menu.
	    */
	    for (int i = index ; i < nitems; i++) {
	        tempItems.addElement(getItem(index));
		remove(index);
	    }

	    add(menuitem);

	    /* Add the removed items back to the menu, they are
	       already in the correct order in the temp vector.
	    */
	    for (int i = 0; i < tempItems.size()  ; i++) {
	        add((MenuItem)tempItems.elementAt(i));
	    }
	}
    
public voidinsert(java.lang.String label, int index)
Inserts a menu item with the specified label into this menu at the specified position. This is a convenience method for insert(menuItem, index).

param
label the text on the item
param
index the position at which the menu item should be inserted
see
java.awt.Menu#add(java.lang.String)
see
java.awt.Menu#add(java.awt.MenuItem)
exception
IllegalArgumentException if the value of index is less than zero
since
JDK1.1

        insert(new MenuItem(label), index);
    
public voidinsertSeparator(int index)
Inserts a separator at the specified position.

param
index the position at which the menu separator should be inserted.
exception
IllegalArgumentException if the value of index is less than 0.
see
java.awt.Menu#addSeparator
since
JDK1.1

        synchronized (getTreeLock()) {
	    if (index < 0) {
	        throw new IllegalArgumentException("index less than zero.");
	    }

	    int nitems = getItemCount();
	    Vector tempItems = new Vector();

	    /* Remove the item at index, nitems-index times 
	       storing them in a temporary vector in the
	       order they appear on the menu.
	    */
	    for (int i = index ; i < nitems; i++) {
	        tempItems.addElement(getItem(index));
		remove(index);
	    }

	    addSeparator();

	    /* Add the removed items back to the menu, they are
	       already in the correct order in the temp vector.
	    */
	    for (int i = 0; i < tempItems.size()  ; i++) {
	        add((MenuItem)tempItems.elementAt(i));
	    }
	}
    
public booleanisTearOff()
Indicates whether this menu is a tear-off menu.

Tear-off functionality may not be supported by all implementations of AWT. If a particular implementation doesn't support tear-off menus, this value is silently ignored.

return
true if this is a tear-off menu; false otherwise.

	return tearOff;
    
public java.lang.StringparamString()
Returns a string representing the state of this Menu. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be null.

return
the parameter string of this menu

        String str = ",tearOff=" + tearOff+",isHelpMenu=" + isHelpMenu;
        return super.paramString() + str;
    
private voidreadObject(java.io.ObjectInputStream s)
Reads the ObjectInputStream. Unrecognized keys or values will be ignored.

param
s the ObjectInputStream to read
exception
HeadlessException if GraphicsEnvironment.isHeadless returns true
see
java.awt.GraphicsEnvironment#isHeadless
see
#writeObject(ObjectOutputStream)

      // HeadlessException will be thrown from MenuComponent's readObject
      s.defaultReadObject();
      for(int i = 0; i < items.size(); i++) {
	MenuItem item = (MenuItem)items.elementAt(i);
	item.parent = this;
      }
    
public voidremove(int index)
Removes the menu item at the specified index from this menu.

param
index the position of the item to be removed.

        synchronized (getTreeLock()) {
	    MenuItem mi = getItem(index);
	    items.removeElementAt(index);
	    MenuPeer peer = (MenuPeer)this.peer;
	    if (peer != null) {
	        mi.removeNotify();
		mi.parent = null;
		peer.delItem(index);
	    }
	}
    
public voidremove(java.awt.MenuComponent item)
Removes the specified menu item from this menu.

param
item the item to be removed from the menu. If item is null or is not in this menu, this method does nothing.

        synchronized (getTreeLock()) {
	    int index = items.indexOf(item);
	    if (index >= 0) {
	        remove(index);
	    }
	}
    
public voidremoveAll()
Removes all items from this menu.

since
JDK1.0.

        synchronized (getTreeLock()) {
	    int nitems = getItemCount();
	    for (int i = nitems-1 ; i >= 0 ; i--) {
	        remove(i);
	    }
	}
    
public voidremoveNotify()
Removes the menu's peer. The peer allows us to modify the appearance of the menu without changing its functionality.

        synchronized (getTreeLock()) {
	    int nitems = getItemCount();
	    for (int i = 0 ; i < nitems ; i++) {
	        getItem(i).removeNotify();
	    }
	    super.removeNotify();
	}
    
synchronized java.util.Enumerationshortcuts()

        Vector shortcuts = new Vector();
        int nitems = getItemCount();
	for (int i = 0 ; i < nitems ; i++) {
            MenuItem mi = getItem(i);
            if (mi instanceof Menu) {
                Enumeration e = ((Menu)mi).shortcuts();
                while (e.hasMoreElements()) {
                    shortcuts.addElement(e.nextElement());
                }
            } else {
                MenuShortcut ms = mi.getShortcut();
                if (ms != null) {
                    shortcuts.addElement(ms);
                }
            }
	}
        return shortcuts.elements();
    
private voidwriteObject(java.io.ObjectOutputStream s)
Writes default serializable fields to stream.

param
s the ObjectOutputStream to write
see
AWTEventMulticaster#save(ObjectOutputStream, String, EventListener)
see
#readObject(ObjectInputStream)


                           
       
       
    
      s.defaultWriteObject();