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


How to Use Choices

The Choice(in the API reference documentation) class provides a menu-like list of choices, accessed by a distinctive button. The user presses the button to bring up the "menu", and then chooses one of the items. Another name you might know for this UI element is pop-up list. Other ways of providing multiple alternatives are checkboxes, lists, and menus.

Below is an applet that has a Choice and a Label. When the user chooses an item from the Choice list, the Label changes to reflect the item chosen.


Your browser doesn't understand the <APPLET> tag, so here are some snapshots of choices.

Here's what this page's applet looks like at first:

Here's what the applet looks like as you select another item in the choice:

Finally, here's what the applet looks like after the other item is selected:


Below is the code that creates the Choice and handles events from it. (Here's the whole program.) Note that the second parameter to the action() method (which is the same as e.arg), is the string from the selected item.

    //...Where instance variables are defined:
    Choice choice; //pop-up list of choices

    //...Where initialization occurs:
    choice = new Choice();
    choice.addItem("ichi");
    choice.addItem("ni");
    choice.addItem("san");
    choice.addItem("shi");
    label = new Label();
    setLabelText(choice.getSelectedIndex(), choice.getSelectedItem());

. . .

public boolean action(Event e, Object arg) {
    if (e.target instanceof Choice) {
	    setLabelText(choice.getSelectedIndex(), (String)arg);
	    return true;
	}
	return false;
    }
}


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