MenuBar
The MenuBar
is the component you add to the Frame
that is displayed on the top line of the Frame
; the MenuBar
contains menus. A Frame
can display only one MenuBar
at a time. However, you can change the MenuBar
based on the state of the program so that different menus can appear at different points. The MenuBar
class extends MenuComponent
and implements the MenuContainer
interface.
A MenuBar
can be used only as a child component of a Frame
. An applet cannot have a MenuBar
attached to it, unless you implement the whole thing yourself. Normally, you cannot modify the MenuBar
of the applet holder (the browser), unless it is Java based. In other words, you cannot affect the menus of Netscape Navigator, but you can customize appletviewer and HotJava, as shown in the following code with the result shown in Figure 10.4. The getTopLevelParent()
method was introduced in Window with Window
.
import java.awt.*; public class ChangeMenu extends java.applet.Applet { public void init () { Frame f = ComponentUtilities.getTopLevelParent(this); if (f != null) { MenuBar mb = f.getMenuBar(); Menu m = new Menu ("Cool"); mb.add (m); } } }
Figure 10.4: Customizing appletviewer's MenuBar
NOTE:
When you add a MenuBar
to a Frame
, it takes up space that is part of the drawing area. You need to get the top insets to find out how much space is occupied by the MenuBar
and be careful not to draw under it. If you do, the MenuBar
will cover what you draw.
MenuBar Methods
Constructors- public MenuBar()
- The
MenuBar
constructor creates an emptyMenuBar
. To add menus to theMenuBar
, use theadd()
method.
- public int getMenuCount ()
public int countMenus () - The
getMenuCount()
method returns the number of top-level menus within theMenuBar
.countMenus()
is the Java 1.0 name for this method. - public Menu getMenu (int index)
- The
getMenu()
method returns theMenu
at positionindex
. Ifindex
is invalid,getMenu()
throws the run-time exceptionArrayIndexOutOfBoundsException
. - public synchronized Menu add (Menu m)
- The
add()
method puts choicem
on theMenuBar
. The label used to createm
is displayed on theMenuBar
. Ifm
is already in anotherMenuBar
, it is removed from it. The order of items added determines the order displayed on theMenuBar
, with one exception: if a menu is designated as a help menu bysetHelpMenu()
, it is placed at the right end of the menu bar. Only aMenu
can be added to aMenuBar
; you can't add aMenuItem
. In other words, aMenuItem
has to lie under at least one menu. - public synchronized void remove (int index)
- The
remove()
method removes theMenu
at positionindex
from theMenuBar
. Ifindex
is invalid,remove()
throws theArrayIndexOutOfBoundsException
run-time exception.index
is zero based. - public synchronized void remove (MenuComponent component)
- This version of
remove()
removes the menucomponent
from theMenuBar
. Ifcomponent
is not inMenuBar
, nothing happens. The system calls this method when you add a newMenu
to make sure it does not exist on anotherMenuBar
.
- public MenuItem getShortcutMenuItem (MenuShortcut shortcut)
- The
getShortcutMenuItem()
method retrieves theMenuItem
associated with theMenuShortcut
shortcut
. IfMenuShortcut
does not exist for thisMenu
, the method returnsnull
.getShortcutMenuItem()
walks through the all submenus recursively to try to findshortcut
. - public synchronized Enumeration shortcuts()
- The
shortcuts()
method retrieves anEnumeration
of all theMenuShortcut
objects associated with thisMenuBar
. - public void deleteShortcut (MenuShortcut shortcut)
- The
deleteShortcut()
method removesMenuShortcut
from the associatedMenuItem
in theMenuBar
. If the shortcut is not associated with any menu item, nothing happens.
It is the convention on many platforms to display help menus as the last menu on the MenuBar
. The MenuBar
class lets you designate one of the menus as this special menu. The physical position of a help menu depends on the platform, but those giving special treatment to help menus place them on the right. A Menu
designated as a help menu doesn't have to bear the label "Help"; the label is up to you.
- public Menu getHelpMenu ()
- The
getHelpMenu()
method returns theMenu
that has been designated as the help menu withsetHelpMenu()
. If the menu bar doesn't have a help menu,getHelpMenu()
returnsnull
. - public synchronized void setHelpMenu (Menu m)
- The
setHelpMenu()
method sets the menu bar's help menu tom
. This makesm
the rightmost menu on theMenuBar
, possibly right justified. Ifm
is not already on theMenuBar
, nothing happens.
- public synchronized void addNotify ()
- The
addNotify()
method creates theMenuBar
peer with all the menus on it, and in turn their menu items. - public synchronized void removeNotify ()
- The
removeNotify()
method destroys the peer of theMenuBar
and removes it from the screen. The peers of the items on theMenuBar
are also destroyed.
MenuBar Events
A MenuBar
does not generate any events.