MenuBar (Java Platform SE 8 ) (original) (raw)
- java.awt.MenuComponent
- java.awt.MenuBar
All Implemented Interfaces:
MenuContainer, Serializable, Accessible
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:
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,shortcuts() andgetShortcutMenuItem(java.awt.MenuShortcut) that retrieve information about the shortcuts a given menu bar is managing.
Since:
JDK1.0
See Also:
Frame, Frame.setMenuBar(java.awt.MenuBar), Menu, MenuItem, MenuShortcut, Serialized Form
Nested Class Summary
Nested Classes
Modifier and Type Class Description protected class MenuBar.AccessibleAWTMenuBar Inner class of MenuBar used to provide default support for accessibility. * ### Nested classes/interfaces inherited from class java.awt.[MenuComponent](../../java/awt/MenuComponent.html "class in java.awt") `[MenuComponent.AccessibleAWTMenuComponent](../../java/awt/MenuComponent.AccessibleAWTMenuComponent.html "class in java.awt")`
Constructor Summary
Constructors
Constructor Description MenuBar() Creates a new menu bar. Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods
Modifier and Type Method Description Menu add(Menu m) Adds the specified menu to the menu bar. void addNotify() Creates the menu bar's peer. int countMenus() Deprecated. void deleteShortcut(MenuShortcut s) Deletes the specified menu shortcut. AccessibleContext getAccessibleContext() Gets the AccessibleContext associated with this MenuBar. Menu getHelpMenu() Gets the help menu on the menu bar. Menu getMenu(int i) Gets the specified menu. int getMenuCount() Gets the number of menus on the menu bar. 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. void remove(int index) Removes the menu located at the specified index from this menu bar. void remove(MenuComponent m) Removes the specified menu component from this menu bar. void removeNotify() Removes the menu bar's peer. void setHelpMenu(Menu m) Sets the specified menu to be this menu bar's help menu. Enumeration<MenuShortcut> shortcuts() Gets an enumeration of all menu shortcuts this menu bar is managing. * ### Methods inherited from class java.awt.[MenuComponent](../../java/awt/MenuComponent.html "class in java.awt") `[dispatchEvent](../../java/awt/MenuComponent.html#dispatchEvent-java.awt.AWTEvent-), [getFont](../../java/awt/MenuComponent.html#getFont--), [getName](../../java/awt/MenuComponent.html#getName--), [getParent](../../java/awt/MenuComponent.html#getParent--), [getPeer](../../java/awt/MenuComponent.html#getPeer--), [getTreeLock](../../java/awt/MenuComponent.html#getTreeLock--), [paramString](../../java/awt/MenuComponent.html#paramString--), [postEvent](../../java/awt/MenuComponent.html#postEvent-java.awt.Event-), [processEvent](../../java/awt/MenuComponent.html#processEvent-java.awt.AWTEvent-), [setFont](../../java/awt/MenuComponent.html#setFont-java.awt.Font-), [setName](../../java/awt/MenuComponent.html#setName-java.lang.String-), [toString](../../java/awt/MenuComponent.html#toString--)` * ### Methods inherited from class java.lang.[Object](../../java/lang/Object.html "class in java.lang") `[clone](../../java/lang/Object.html#clone--), [equals](../../java/lang/Object.html#equals-java.lang.Object-), [finalize](../../java/lang/Object.html#finalize--), [getClass](../../java/lang/Object.html#getClass--), [hashCode](../../java/lang/Object.html#hashCode--), [notify](../../java/lang/Object.html#notify--), [notifyAll](../../java/lang/Object.html#notifyAll--), [wait](../../java/lang/Object.html#wait--), [wait](../../java/lang/Object.html#wait-long-), [wait](../../java/lang/Object.html#wait-long-int-)` * ### Methods inherited from interface java.awt.[MenuContainer](../../java/awt/MenuContainer.html "interface in java.awt") `[getFont](../../java/awt/MenuContainer.html#getFont--), [postEvent](../../java/awt/MenuContainer.html#postEvent-java.awt.Event-)`
Constructor Detail
* #### MenuBar public MenuBar() throws [HeadlessException](../../java/awt/HeadlessException.html "class in java.awt") Creates a new menu bar. Throws: `[HeadlessException](../../java/awt/HeadlessException.html "class in java.awt")` \- if GraphicsEnvironment.isHeadless() returns true. See Also: [GraphicsEnvironment.isHeadless()](../../java/awt/GraphicsEnvironment.html#isHeadless--)
Method Detail
* #### addNotify 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. * #### removeNotify public void 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. Overrides: `[removeNotify](../../java/awt/MenuComponent.html#removeNotify--)` in class `[MenuComponent](../../java/awt/MenuComponent.html "class in java.awt")` * #### getHelpMenu public [Menu](../../java/awt/Menu.html "class in java.awt") getHelpMenu() Gets the help menu on the menu bar. Returns: the help menu on this menu bar. * #### setHelpMenu public void setHelpMenu([Menu](../../java/awt/Menu.html "class in java.awt") 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` \- the menu to be set as the help menu * #### add public [Menu](../../java/awt/Menu.html "class in java.awt") add([Menu](../../java/awt/Menu.html "class in java.awt") 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` \- the menu to be added Returns: the menu added See Also: [remove(int)](../../java/awt/MenuBar.html#remove-int-), [remove(java.awt.MenuComponent)](../../java/awt/MenuBar.html#remove-java.awt.MenuComponent-) * #### remove public void remove(int index) Removes the menu located at the specified index from this menu bar. Parameters: `index` \- the position of the menu to be removed. See Also: [add(java.awt.Menu)](../../java/awt/MenuBar.html#add-java.awt.Menu-) * #### remove public void remove([MenuComponent](../../java/awt/MenuComponent.html "class in java.awt") m) Removes the specified menu component from this menu bar. Specified by: `[remove](../../java/awt/MenuContainer.html#remove-java.awt.MenuComponent-)` in interface `[MenuContainer](../../java/awt/MenuContainer.html "interface in java.awt")` Parameters: `m` \- the menu component to be removed. See Also: [add(java.awt.Menu)](../../java/awt/MenuBar.html#add-java.awt.Menu-) * #### getMenuCount public int getMenuCount() Gets the number of menus on the menu bar. Returns: the number of menus on the menu bar. Since: JDK1.1 * #### countMenus [@Deprecated](../../java/lang/Deprecated.html "annotation in java.lang") public int countMenus() Deprecated. * #### getMenu public [Menu](../../java/awt/Menu.html "class in java.awt") getMenu(int i) Gets the specified menu. Parameters: `i` \- the index position of the menu to be returned. Returns: the menu at the specified index of this menu bar. * #### shortcuts public [Enumeration](../../java/util/Enumeration.html "interface in java.util")<[MenuShortcut](../../java/awt/MenuShortcut.html "class in java.awt")> shortcuts() Gets an enumeration of all menu shortcuts this menu bar is managing. Returns: an enumeration of menu shortcuts that this menu bar is managing. Since: JDK1.1 See Also: [MenuShortcut](../../java/awt/MenuShortcut.html "class in java.awt") * #### getShortcutMenuItem public [MenuItem](../../java/awt/MenuItem.html "class in java.awt") getShortcutMenuItem([MenuShortcut](../../java/awt/MenuShortcut.html "class in java.awt") 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` \- the specified menu shortcut. Since: JDK1.1 See Also: [MenuItem](../../java/awt/MenuItem.html "class in java.awt"), [MenuShortcut](../../java/awt/MenuShortcut.html "class in java.awt") * #### deleteShortcut public void deleteShortcut([MenuShortcut](../../java/awt/MenuShortcut.html "class in java.awt") s) Deletes the specified menu shortcut. Parameters: `s` \- the menu shortcut to delete. Since: JDK1.1 * #### getAccessibleContext public [AccessibleContext](../../javax/accessibility/AccessibleContext.html "class in javax.accessibility") 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. Specified by: `[getAccessibleContext](../../javax/accessibility/Accessible.html#getAccessibleContext--)` in interface `[Accessible](../../javax/accessibility/Accessible.html "interface in javax.accessibility")` Overrides: `[getAccessibleContext](../../java/awt/MenuComponent.html#getAccessibleContext--)` in class `[MenuComponent](../../java/awt/MenuComponent.html "class in java.awt")` Returns: an AccessibleAWTMenuBar that serves as the AccessibleContext of this MenuBar Since: 1.3
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2025, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.