Choice entries produce pull-down menus with a single selection showing, but where the other options get displayed when the user clicks with the mouse. These are sometimes known as "combo boxes," "drop-down list boxes," or "option menus," depending on the operating/windowing system you are using. Unlike buttons and checkboxes, the entire Choice is not created using the constructor. Instead, there is a two-step process. The first step builds an empty menu, and the second adds elements to it using addItem (Java 1.0) or add (Java 1.1). Creating a Choice menu and adding it to a panel is illustrated as follows:
Choice choice = new Choice();
choice.addItem("...");
choice.addItem("...");
...
somePanel.add(choice);
This creates an empty combo box. Menu entries are not added in the constructor, but in a separate step via addItem (Java 1.0) or add (Java 1.1).
Listing 13.17 creates a simple Choice with three entries. Figures 13-14 through 13-17 show the results in appletviewer on various operating systems.
Listing 13.17
|
|---|
import java.applet.Applet;
import java.awt.*;
public class ChoiceTest extends Applet {
private Choice choice;
public void init() {
setFont(new Font("Helvetica", Font.BOLD, 36));
choice = new Choice();
choice.addItem("Choice 1");
choice.addItem("Choice 2");
choice.addItem("Choice 3");
add(choice);
}
}
|
Choice
menu (unselected) on Windows 95.
Choice
menu (selected) on Windows 95.
Choice
menu (selected) on MacOS.
Choice
menu (unselected) on Solaris.
A Choice menu is built in two steps. First, an empty menu is created using the empty constructor. Second, entries are added to the menu via addItem (or by add in Java 1.1). By default, the first item added is the one initially displayed, but you can override this by using select. In JDK 1.02 there is no mechanism for removing entries once they are added, but in JDK 1.1 you can use remove or removeAll.
This method lets you attach/detach an ItemListener to process selection events.
These methods return the number of entries in the Choice.
This returns the label of the item at the specified index.
This returns the index of the item that is currently selected. -1 is returned if the Choice has no entries.
This returns the label of the currently-selected item.
This Java 1.1 method returns an array containing either the selected entry or null if no entry is selected.
The insert method adds an entry to the specified location in the list. This is in contrast to add or addItem, which add to the end of the list only.
You can override this lower-level event-processing method if you want to have the Choice handle its own events.
These methods remove entries from the combo box. Surprisingly, there is no way to do this in version 1.0 of Java.
This selects the item at the specified index. Recall that Java uses zero-based indexing.
Select the first item with the specified label. No action is taken if no such label exists.
As usual, recall that the color, font, size, visibility, etc., methods of Component (Section 11.2) are inherited. Also as usual, note that the use of color varies greatly across implementations. For instance, on Windows 95/NT, neither Netscape 2, Internet Explorer 3, nor Sun JDK 1.02 support colors for Choice menus. Netscape 3 on Windows supports colors for the initial display, but uses the default colors for the popup menu when the Choice is selected. Netscape 4 and Sun JDK 1.1 fully support Choice colors on Windows. Netscape 3 on MacOS supports neither colors nor custom fonts for Choice menus. On Solaris, neither Netscape 2 nor 3 supports Choice colors. Sun/Solaris JDK 1.02 supports background colors only; JDK 1.1 fully supports Choice colors.
Events are handled in a manner very similar to checkboxes. In Java 1.02, you override the action method in the Choice or in an enclosing window. In release 1.1 of Java, you override processItemEvent or use addItemListener to attach an external listener. The main difference is that with checkboxes you have only two possible values (selected or deselected). With choice menus, you can have an arbitrary number of options. So after determining that the checkbox was acted upon, you have to next decide which entry was chosen. Listing 13.18 gives an expanded version of the ChoiceTest class shown earlier. Here, the Java 1.0 event-processing model is used to print out which entry was selected. Note that the second argument to the action method is the label of the Choice; you can use that instead of getSelectedItem if you want. Also, as with checkboxes, it is quite common to ignore the selection event when it occurs and simply determine which item is selected via getSelectedItem or getSelectedIndex when it is needed.
Listing 13.18
|
|---|
import java.applet.Applet;
import java.awt.*;
public class ChoiceTest extends Applet {
private Choice choice;
public void init() {
setFont(new Font("Helvetica", Font.BOLD, 36));
choice = new Choice();
choice.addItem("Choice 1");
choice.addItem("Choice 2");
choice.addItem("Choice 3");
add(choice);
}
public boolean action(Event event, Object object) {
if (event.target == choice) {
String selection = choice.getSelectedItem();
if (selection.equals("Choice 1"))
doChoice1Action();
else if (selection.equals("Choice 2"))
doChoice2Action();
else if (selection.equals("Choice 3"))
doChoice3Action();
return(true);
} else
return(false);
}
private void doChoice1Action() {
System.out.println("Choice 1 Action");
}
private void doChoice2Action() {
System.out.println("Choice 2 Action");
}
private void doChoice3Action() {
System.out.println("Choice 3 Action");
}
}
|
| Continue to Section 13.7 (List Boxes). | Return to Chapter 13 table of contents. |
|---|