FileDocCategorySizeDatePackage
MenuBar.javaAPI DocJava SE 5 API13766Fri Aug 26 14:56:46 BST 2005java.awt

MenuBar

public class MenuBar extends MenuComponent implements MenuContainer, Accessible
The MenuBar class encapsulates the platform's concept of a menu bar bound to a frame. In order to associate the menu bar with a Frame object, call the frame's setMenuBar method.

This is what a menu bar might look like:

ALIGN=center HSPACE=10 VSPACE=7>

A menu bar handles keyboard shortcuts for menu items, passing them along to its child menus. (Keyboard shortcuts, which are optional, provide the user with an alternative to the mouse for invoking a menu item and the action that is associated with it.) Each menu item can maintain an instance of MenuShortcut. The MenuBar class defines several methods, {@link MenuBar#shortcuts} and {@link MenuBar#getShortcutMenuItem} that retrieve information about the shortcuts a given menu bar is managing.

version
1.69, 05/18/04
author
Sami Shaio
see
java.awt.Frame
see
java.awt.Frame#setMenuBar(java.awt.MenuBar)
see
java.awt.Menu
see
java.awt.MenuItem
see
java.awt.MenuShortcut
since
JDK1.0

Fields Summary
Vector
menus
This field represents a vector of the actual menus that will be part of the MenuBar.
Menu
helpMenu
This menu is a special menu dedicated to help. The one thing to note about this menu is that on some platforms it appears at the right edge of the menubar.
private static final String
base
private static int
nameCounter
private static final long
serialVersionUID
private int
menuBarSerializedDataVersion
The MenuBar's serialized data version.
Constructors Summary
public MenuBar()
Creates a new menu bar.

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


                      
        
    
Methods Summary
public java.awt.Menuadd(java.awt.Menu m)
Adds the specified menu to the menu bar. If the menu has been part of another menu bar, removes it from that menu bar.

param
m the menu to be added
return
the menu added
see
java.awt.MenuBar#remove(int)
see
java.awt.MenuBar#remove(java.awt.MenuComponent)

        synchronized (getTreeLock()) {
	    if (m.parent != null) {
	        m.parent.remove(m);
	    }
	    menus.addElement(m);
	    m.parent = this;

	    MenuBarPeer peer = (MenuBarPeer)this.peer;
	    if (peer != null) {
	        if (m.peer == null) {
		    m.addNotify();
		}
		peer.addMenu(m);
	    }
	    return m;
	}
    
public voidaddNotify()
Creates the menu bar's peer. The peer allows us to change the appearance of the menu bar without changing any of the menu bar's functionality.

        synchronized (getTreeLock()) {
	    if (peer == null)
	        peer = Toolkit.getDefaultToolkit().createMenuBar(this);

	    int nmenus = getMenuCount();
	    for (int i = 0 ; i < nmenus ; i++) {
	        getMenu(i).addNotify();
	    }
	}
    
java.lang.StringconstructComponentName()
Construct a name for this MenuComponent. Called by getName() when the name is null.

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

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

	return getMenuCountImpl();
    
public voiddeleteShortcut(java.awt.MenuShortcut s)
Deletes the specified menu shortcut.

param
s the menu shortcut to delete.
since
JDK1.1

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

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

return
an AccessibleAWTMenuBar that serves as the AccessibleContext of this MenuBar

        if (accessibleContext == null) {
            accessibleContext = new AccessibleAWTMenuBar();
        }
        return accessibleContext;
    
public java.awt.MenugetHelpMenu()
Gets the help menu on the menu bar.

return
the help menu on this menu bar.

	return helpMenu;
    
public java.awt.MenugetMenu(int i)
Gets the specified menu.

param
i the index position of the menu to be returned.
return
the menu at the specified index of this menu bar.

	return getMenuImpl(i);
    
public intgetMenuCount()
Gets the number of menus on the menu bar.

return
the number of menus on the menu bar.
since
JDK1.1

	return countMenus();
    
final intgetMenuCountImpl()

	return menus.size();
    
final java.awt.MenugetMenuImpl(int i)

	return (Menu)menus.elementAt(i);
    
public java.awt.MenuItemgetShortcutMenuItem(java.awt.MenuShortcut s)
Gets the instance of MenuItem associated with the specified MenuShortcut object, or null if none of the menu items being managed by this menu bar is associated with the specified menu shortcut.

param
s the specified menu shortcut.
see
java.awt.MenuItem
see
java.awt.MenuShortcut
since
JDK1.1

	int nmenus = getMenuCount();
	for (int i = 0 ; i < nmenus ; i++) {
            MenuItem mi = getMenu(i).getShortcutMenuItem(s);
            if (mi != null) {
                return mi;
            }
	}
        return null;  // MenuShortcut wasn't found
     
booleanhandleShortcut(java.awt.event.KeyEvent e)

        // Is it a key event?
        int id = e.getID();
        if (id != KeyEvent.KEY_PRESSED && id != KeyEvent.KEY_RELEASED) {
            return false;
        }

        // Is the accelerator modifier key pressed?
        int accelKey = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
        if ((e.getModifiers() & accelKey) == 0) {
            return false;
        }

        // Pass MenuShortcut on to child menus.
	int nmenus = getMenuCount();
	for (int i = 0 ; i < nmenus ; i++) {
	    Menu m = getMenu(i);
            if (m.handleShortcut(e)) {
                return true;
            }
        }
        return false;
    
private static native voidinitIDs()
Initialize JNI field and method IDs

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(java.io.ObjectOutputStream)

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

param
index the position of the menu to be removed.
see
java.awt.MenuBar#add(java.awt.Menu)

        synchronized (getTreeLock()) {
            Menu m = getMenu(index);
            menus.removeElementAt(index);
	    MenuBarPeer peer = (MenuBarPeer)this.peer;
	    if (peer != null) {
		m.removeNotify();
		m.parent = null;
		peer.delMenu(index);
	    }
	}
    
public voidremove(java.awt.MenuComponent m)
Removes the specified menu component from this menu bar.

param
m the menu component to be removed.
see
java.awt.MenuBar#add(java.awt.Menu)

        synchronized (getTreeLock()) {
	    int index = menus.indexOf(m);
	    if (index >= 0) {
	        remove(index);
	    }
	}
    
public voidremoveNotify()
Removes the menu bar's peer. The peer allows us to change the appearance of the menu bar without changing any of the menu bar's functionality.

        synchronized (getTreeLock()) {
	    int nmenus = getMenuCount();
	    for (int i = 0 ; i < nmenus ; i++) {
	        getMenu(i).removeNotify();
	    }
	    super.removeNotify();
	}
    
public voidsetHelpMenu(java.awt.Menu m)
Sets the specified menu to be this menu bar's help menu. If this menu bar has an existing help menu, the old help menu is removed from the menu bar, and replaced with the specified menu.

param
m the menu to be set as the help menu

        synchronized (getTreeLock()) {
	    if (helpMenu == m) {
	        return;
	    }
	    if (helpMenu != null) {
                remove(helpMenu);
	    }
	    if (m.parent != this) {
	        add(m);
	    }
	    helpMenu = m;
	    if (m != null) {
	        m.isHelpMenu = true;
		m.parent = this;
		MenuBarPeer peer = (MenuBarPeer)this.peer;
		if (peer != null) {
		    if (m.peer == null) {
		        m.addNotify();
		    }
		    peer.addHelpMenu(m);
		}
	    }
	}
    
public synchronized java.util.Enumerationshortcuts()
Gets an enumeration of all menu shortcuts this menu bar is managing.

return
an enumeration of menu shortcuts that this menu bar is managing.
see
java.awt.MenuShortcut
since
JDK1.1

        Vector shortcuts = new Vector();
	int nmenus = getMenuCount();
	for (int i = 0 ; i < nmenus ; i++) {
            Enumeration e = getMenu(i).shortcuts();
            while (e.hasMoreElements()) {
                shortcuts.addElement(e.nextElement());
            }
	}
        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(java.io.ObjectInputStream)


                           
       
       
	     
    
      s.defaultWriteObject();