APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.awt

public Class MenuBar

extends MenuComponent
implements MenuContainer, Accessible
Class Inheritance
All Implemented Interfaces
javax.accessibility.Accessible, java.awt.MenuContainer
Imports
java.awt.event.KeyEvent, java.awt.peer.MenuBarPeer, java.io.IOException, .ObjectInputStream, .ObjectOutputStream, .Serial, java.util.Enumeration, .EventListener, .Vector, javax.accessibility.Accessible, .AccessibleContext, .AccessibleRole, sun.awt.AWTAccessor

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:

Diagram of MenuBar containing 2 menus: Examples and Options. Examples
menu is expanded showing items: Basic, Simple, Check, and More Examples.

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, MenuBar#shortcuts and MenuBar#getShortcutMenuItem that retrieve information about the shortcuts a given menu bar is managing.

Author
Sami Shaio
Since
1.0
See Also
java.awt.Frame, java.awt.Frame#setMenuBar(java.awt.MenuBar), java.awt.Menu, java.awt.MenuItem, java.awt.MenuShortcut

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
MenuBar.AccessibleAWTMenuBar

Inner class of MenuBar used to provide default support for accessibility.

Field Summary

Modifier and TypeField and Description
private static final String
private volatile Menu
helpMenu

This menu is a special menu dedicated to help.

private static int
private static final long
serialVersionUID

Hides java.awt.MenuComponent.serialVersionUID.

Use serialVersionUID from JDK 1.1 for interoperability.
Inherited from java.awt.MenuComponent:
accessibleContextactionListenerKitemListenerKnewEventsOnlyparentpeer

Constructor Summary

AccessConstructor and Description

Method Summary

Modifier and TypeMethod and Description
public Menu

Returns:

the menu added
add
(Menu
the menu to be added
m
)

Adds the specified menu to the menu bar.

public void
addNotify()

Creates the menu bar's peer.

pack-priv String
constructComponentName()

Overrides java.awt.MenuComponent.constructComponentName.

Construct a name for this MenuComponent.

public int

Returns:

the number of menus on the menu bar.
countMenus
()
Deprecated As of JDK version 1.1, replaced by getMenuCount().

Gets the number of menus on the menu bar.

public void
deleteShortcut(MenuShortcut
the menu shortcut to delete.
s
)

Deletes the specified menu shortcut.

pack-priv int
public AccessibleContext

Returns:

an AccessibleAWTMenuBar that serves as the AccessibleContext of this MenuBar
getAccessibleContext
()

Overrides java.awt.MenuComponent.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

Gets the AccessibleContext associated with this MenuBar.

public Menu

Returns:

the help menu on this menu bar.
getHelpMenu
()

Gets the help menu on the menu bar.

public Menu

Returns:

the menu at the specified index of this menu bar.
getMenu
(int
the index position of the menu to be returned.
i
)

Gets the specified menu.

public int

Returns:

the number of menus on the menu bar.
getMenuCount
()

Gets the number of menus on the menu bar.

pack-priv final int
pack-priv final Menu
getMenuImpl(int i)

public MenuItem

Returns:

the menu item for the specified shortcut.
getShortcutMenuItem
(MenuShortcut
the specified menu shortcut.
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.

pack-priv boolean
private static native void
initIDs()

Initialize JNI field and method IDs

private void
readObject(ObjectInputStream
the ObjectInputStream to read
s
)

Hides java.awt.MenuComponent.readObject.

Reads the ObjectInputStream.

public void
remove(final int
the position of the menu to be removed.
index
)

Removes the menu located at the specified index from this menu bar.

public void
remove(MenuComponent
the menu component to be removed.
m
)

Implements java.awt.MenuContainer.remove.

Removes the specified menu component from this menu bar.

public void
removeNotify()

Overrides java.awt.MenuComponent.removeNotify.

Removes the menu bar's peer.

public void
setHelpMenu(final Menu
the menu to be set as the help menu
m
)

Sets the specified menu to be this menu bar's help menu.

public synchronized Enumeration<MenuShortcut>

Returns:

an enumeration of menu shortcuts that this menu bar is managing.
shortcuts
()

Gets an enumeration of all menu shortcuts this menu bar is managing.

private void
writeObject(ObjectOutputStream
the ObjectOutputStream to write
s
)

Writes default serializable fields to stream.

Inherited from java.awt.MenuComponent:
dispatchEventdispatchEventImpleventEnabledgetAccessibleIndexInParentgetAccessibleStateSetgetComponentFactorygetFontgetFont_NoClientCodegetNamegetParentgetParent_NoClientCodegetTreeLockparamStringpostEventprocessEventsetFontsetNametoString

Field Detail

baseback to summary
private static final String base
helpMenuback to summary
private volatile 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.

See Also
getHelpMenu(), setHelpMenu(Menu)
nameCounterback to summary
private static int nameCounter
serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.awt.MenuComponent.serialVersionUID.

Use serialVersionUID from JDK 1.1 for interoperability.

Annotations
@Serial

Constructor Detail

Method Detail

addback to summary
public Menu add(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.

Parameters
m:Menu

the menu to be added

Returns:Menu

the menu added

See Also
java.awt.MenuBar#remove(int), java.awt.MenuBar#remove(java.awt.MenuComponent)
addNotifyback to summary
public void addNotify()

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.

constructComponentNameback to summary
pack-priv String constructComponentName()

Overrides java.awt.MenuComponent.constructComponentName.

Construct a name for this MenuComponent. Called by getName() when the name is null.

countMenusback to summary
public int countMenus()

Deprecated

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

Gets the number of menus on the menu bar.

Returns:int

the number of menus on the menu bar.

Annotations
@Deprecated
deleteShortcutback to summary
public void deleteShortcut(MenuShortcut s)

Deletes the specified menu shortcut.

Parameters
s:MenuShortcut

the menu shortcut to delete.

Since
1.1
getAccessibleChildIndexback to summary
pack-priv int getAccessibleChildIndex(MenuComponent child)

Overrides java.awt.MenuComponent.getAccessibleChildIndex.

Defined in MenuComponent. Overridden here.

getAccessibleContextback to summary
public AccessibleContext getAccessibleContext()

Overrides java.awt.MenuComponent.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

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.

Returns:AccessibleContext

an AccessibleAWTMenuBar that serves as the AccessibleContext of this MenuBar

Since
1.3
getHelpMenuback to summary
public Menu getHelpMenu()

Gets the help menu on the menu bar.

Returns:Menu

the help menu on this menu bar.

getMenuback to summary
public Menu getMenu(int i)

Gets the specified menu.

Parameters
i:int

the index position of the menu to be returned.

Returns:Menu

the menu at the specified index of this menu bar.

getMenuCountback to summary
public int getMenuCount()

Gets the number of menus on the menu bar.

Returns:int

the number of menus on the menu bar.

Since
1.1
getMenuCountImplback to summary
pack-priv final int getMenuCountImpl()
getMenuImplback to summary
pack-priv final Menu getMenuImpl(int i)
getShortcutMenuItemback to summary
public MenuItem getShortcutMenuItem(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.

Parameters
s:MenuShortcut

the specified menu shortcut.

Returns:MenuItem

the menu item for the specified shortcut.

Since
1.1
See Also
java.awt.MenuItem, java.awt.MenuShortcut
handleShortcutback to summary
pack-priv boolean handleShortcut(KeyEvent e)
initIDsback to summary
private static native void initIDs()

Initialize JNI field and method IDs

readObjectback to summary
private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException, HeadlessException

Hides java.awt.MenuComponent.readObject.

Reads the ObjectInputStream. Unrecognized keys or values will be ignored.

Parameters
s:ObjectInputStream

the ObjectInputStream to read

Annotations
@Serial
Exceptions
ClassNotFoundException:
if the class of a serialized object could not be found
IOException:
if an I/O error occurs
HeadlessException:
if GraphicsEnvironment.isHeadless() returns true
See Also
java.awt.GraphicsEnvironment#isHeadless, writeObject(java.io.ObjectOutputStream)
removeback to summary
public void remove(final int index)

Removes the menu located at the specified index from this menu bar.

Parameters
index:int

the position of the menu to be removed.

See Also
java.awt.MenuBar#add(java.awt.Menu)
removeback to summary
public void remove(MenuComponent m)

Implements java.awt.MenuContainer.remove.

Removes the specified menu component from this menu bar.

Parameters
m:MenuComponent

the menu component to be removed.

See Also
java.awt.MenuBar#add(java.awt.Menu)
removeNotifyback to summary
public void removeNotify()

Overrides java.awt.MenuComponent.removeNotify.

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.

setHelpMenuback to summary
public void setHelpMenu(final 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.

Parameters
m:Menu

the menu to be set as the help menu

shortcutsback to summary
public synchronized Enumeration<MenuShortcut> shortcuts()

Gets an enumeration of all menu shortcuts this menu bar is managing.

Returns:Enumeration<MenuShortcut>

an enumeration of menu shortcuts that this menu bar is managing.

Since
1.1
See Also
java.awt.MenuShortcut
writeObjectback to summary
private void writeObject(ObjectOutputStream s) throws IOException

Writes default serializable fields to stream.

Parameters
s:ObjectOutputStream

the ObjectOutputStream to write

Annotations
@Serial
Exceptions
IOException:
if an I/O error occurs
See Also
AWTEventMulticaster#save(ObjectOutputStream, String, EventListener), readObject(java.io.ObjectInputStream)
java.awt back to summary

protected Class MenuBar.AccessibleAWTMenuBar

extends AccessibleAWTMenuComponent
Class Inheritance

Inner class of MenuBar used to provide default support for accessibility. This class is not meant to be used directly by application developers, but is instead meant only to be subclassed by menu component developers.

This class implements accessibility support for the MenuBar class. It provides an implementation of the Java Accessibility API appropriate to menu bar user-interface elements.

Since
1.3

Field Summary

Modifier and TypeField and Description
private static final long
serialVersionUID

Hides java.awt.MenuComponent.AccessibleAWTMenuComponent.serialVersionUID.

Use serialVersionUID from JDK 1.3 for interoperability.

Constructor Summary

AccessConstructor and Description
protected
AccessibleAWTMenuBar()

Constructs an AccessibleAWTMenuBar.

Method Summary

Modifier and TypeMethod and Description
public AccessibleRole

Returns:

an instance of AccessibleRole describing the role of the object
getAccessibleRole
()

Overrides java.awt.MenuComponent.AccessibleAWTMenuComponent.getAccessibleRole.

Get the role of this object.

Inherited from java.awt.MenuComponent.AccessibleAWTMenuComponent:
addAccessibleSelectionaddFocusListenerclearAccessibleSelectioncontainsgetAccessibleAtgetAccessibleChildgetAccessibleChildrenCountgetAccessibleComponentgetAccessibleDescriptiongetAccessibleIndexInParentgetAccessibleNamegetAccessibleParentgetAccessibleSelectiongetAccessibleSelectiongetAccessibleSelectionCountgetAccessibleStateSetgetBackgroundgetBoundsgetCursorgetFontgetFontMetricsgetForegroundgetLocalegetLocationgetLocationOnScreengetSizeisAccessibleChildSelectedisEnabledisFocusTraversableisShowingisVisibleremoveAccessibleSelectionremoveFocusListenerrequestFocusselectAllAccessibleSelectionsetBackgroundsetBoundssetCursorsetEnabledsetFontsetForegroundsetLocationsetSizesetVisible

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.awt.MenuComponent.AccessibleAWTMenuComponent.serialVersionUID.

Use serialVersionUID from JDK 1.3 for interoperability.

Annotations
@Serial

Constructor Detail

AccessibleAWTMenuBarback to summary
protected AccessibleAWTMenuBar()

Constructs an AccessibleAWTMenuBar.

Method Detail

getAccessibleRoleback to summary
public AccessibleRole getAccessibleRole()

Overrides java.awt.MenuComponent.AccessibleAWTMenuComponent.getAccessibleRole.

Get the role of this object.

Returns:AccessibleRole

an instance of AccessibleRole describing the role of the object

Since
1.4