Previous | Next | Trail Map | Creating a User Interface | Using Components, the GUI Building Blocks


How to Use Menus


Your browser doesn't understand the <APPLET> tag. Here's a snapshot of the window the button brings up, after the item in Menu 2 has been selected:

When you press Menu 1, you can see a tear-off indicator:

Selecting it produces a window that has a copy of the menu items:

Menu 2 has an item with a checkbox:

Menu 3 has a separator:


The above applet shows many of the menu features you're likely to use. The window it brings up has a menu bar that contains four menus. Each menu contains one or more items. Menu 1 is a tear-off menu; by clicking the dashed line [implementation-specific?], you create a new window that contains the same menu items as Menu 1. Menu 2's only item has a checkbox. Menu 3 contains a separator between its second and third items. Menu 4 is the window's help menu, which (depending on the platform) generally means that it's set off to the right. When you click on any menu item, the window displays a string indicating which item was clicked and what menu it's in.

The reason the applet brings up a window to demonstrate menus is that the AWT limits where you can use menus. Menus can exist only in menu bars [CHECK], and menu bars can be attached only to windows (specifically, to Frames). [In theory, it's possible to write a MenuContainer that a MenuBar, Menu, or MenuItem can be attached to. Is it possible in fact?]

Menu functionality in the AWT is provided by several classes. These classes do not inherit from Component. Instead, they're subclasses of the MenuComponent(in the API reference documentation) class. [WHY? Tell what MenuComponent provides.] Besides MenuComponent, the AWT provides the following classes to support menus:

MenuItem(in the API reference documentation)
Each item in a menu is represented by a MenuItem object.
CheckboxMenuItem(in the API reference documentation)
Each menu item that contains a checkbox is represented by a CheckboxMenuItem object. CheckboxMenuItem is a subclass of MenuItem.
Menu(in the API reference documentation)
Each menu is represented by a Menu object. Menu is implemented as a subclass of MenuItem, since a menu is to a menu bar what a menu item is to a menu.
MenuBar(in the API reference documentation)
Menu bars are implemented by the MenuBar class. A MenuBar represents the platform-dependent notion of a group of menus attached to a window. MenuBars can not be bound to Panels.

To be able to contain a MenuComponent, an object must adhere to the MenuContainer(in the API reference documentation) interface. The Frame, Menu, and MenuBar classes are the only AWT classes that currently implement MenuContainer.

Here's the code for the window that the above applet brings up. This code can be run as a standalone application or, with the help of the AppletButton class, as an applet. Here's just the code that deals with menus:

public MenuWindow() {
    MenuBar mb;
    Menu m1, m2, m3, m4;
    MenuItem mi1_1, mi1_2, mi3_1, mi3_2, mi3_3, mi3_4, mi4_1, mi4_2;
    CheckboxMenuItem mi2_1;

    // ...Add the output displayer to this window...
    
    //Build the menu bar.
    mb = new MenuBar();
    setMenuBar(mb);

    //Build first menu in the menu bar.
    m1 = new Menu("Menu 1", true);
    mb.add(m1);
    mi1_1 = new MenuItem("Menu Item 1_1");
    m1.add(mi1_1);
    mi1_2 = new MenuItem("Menu Item 1_2");
    m1.add(mi1_2);

    //Build help menu. Note that order in which it's added doesn't matter.
    m4 = new Menu("Menu 4");
    mb.add(m4); //Just setting the help menu doesn't work; must add it.
    mb.setHelpMenu(m4);
    mi4_1 = new MenuItem("Menu Item 4_1");
    m4.add(mi4_1);
    mi4_2 = new MenuItem("Menu Item 4_2");
    m4.add(mi4_2);

    //Build second menu in the menu bar.
    m2 = new Menu("Menu 2");
    mb.add(m2);
    mi2_1 = new CheckboxMenuItem("Menu Item 2_1");
    m2.add(mi2_1);

    //Build third menu in the menu bar.
    m3 = new Menu("Menu 3");
    mb.add(m3);
    mi3_1 = new MenuItem("Menu Item 3_1");
    m3.add(mi3_1);
    mi3_2 = new MenuItem("Menu Item 3_2");
    m3.add(mi3_2);
    m3.addSeparator();
    mi3_3 = new MenuItem("Menu Item 3_3");
    m3.add(mi3_3);
    mi3_4 = new MenuItem("Menu Item 3_4");
    mi3_4.disable();
    m3.add(mi3_4);
}

public boolean action(Event event, Object arg) {
    String str = "Action detected";

    if (event.target instanceof MenuItem) {
	MenuItem mi=(MenuItem)(event.target);
	str += " on " + arg;
	if (mi instanceof CheckboxMenuItem) {
	    str += " (state is " 
	    	   + ((CheckboxMenuItem)mi).getState()
		   + ")";
	}
	MenuContainer parent = mi.getParent();
	if (parent instanceof Menu) {
	    str += " in " + ((Menu)parent).getLabel();
	} else {
	    str += " in a container that isn't a Menu";
	}
    }
    str += ".\n";
    //...Display string in the output area...
    return false;
}
Besides menus, checkboxes, choices, and lists are other ways of presenting options to the user.


Previous | Next | Trail Map | Creating a User Interface | Using Components, the GUI Building Blocks