Using Components, the GUI Building Blocks |
The Choice 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.
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 ase.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; } }
Using Components, the GUI Building Blocks |