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

[Graphic: Figure 10-4]

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 Menus Shortcuts Help menus

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.

Peers

MenuBar Events

A MenuBar does not generate any events.