Pick Me

Contents:
Choice
Lists
Checkbox
CheckboxGroup
ItemSelectable

Three AWT components let you present a list of choices to users: Choice, List, and Checkbox. All three components implement the ItemSelectable interface ( Java1.1). These components are comparable to selection mechanisms in modern GUIs so most readers will be able to learn them easily, but I'll point out some special enhancements that they provide.

Choice and List are similar; both offer a list of choices for the user to select. Choice provides a pull-down list that offers one selection at a time, whereas List is a scrollable list that allows a user to make one or multiple selections. From a design standpoint, which you choose depends at least partially on screen real estate; if you want the user to select from a large group of alternatives, Choice requires the least space, List requires somewhat more, while Checkbox requires the most. Choice is the only component in this group that does not allow multiple selections. A List allows multiple or single selection; because each Checkbox is a separate component, checkboxes inherently allow multiple selection. In order to create a list of mutually exclusive checkboxes, in which only one box can be selected at a time (commonly known as radio buttons), you can put several checkboxes together into a CheckboxGroup, which is discussed at the end of this chapter.

Choice

The Choice component provides pop-up/pull-down lists. It is the equivalent of Motif's OptionMenu or Windows MFC's ComboBox. ( Java 1.1 departs from the MFC world.) With the Choice component, you can provide a short list of choices to the user, while taking up the space of a single item on the screen. When the component is selected, the complete list of available choices appears on the screen. After the user has selected an option, the list is removed from the screen and the selected item is displayed. Selecting any item automatically deselects the previous selection.

Component Methods

Constructors

Figure 9.1: How Choices are displayed

[Graphic: Figure 9-1]Items

Selection

The Choice has one item selected at a time. Initially, it is the first item that was added to the Choice.

Miscellaneous methods
java.awt.Choice[139,5,92x27,current=Dialog] 

Choice Events

The primary event for a Choice occurs when the user selects an item in the list. With the 1.0 event model, selecting an item generates an ACTION_EVENT, which triggers a call to the action() method. Once the Choice has the input focus, the user can change the selection by using the arrow or keyboard keys. The arrow keys scroll through the list of choices, triggering the KEY_ACTION, ACTION_EVENT, and KEY_ACTION_RELEASE event sequence, which in turn invokes the keyDown(), action(), and keyUp() methods, respectively. If the mouse is used to choose an item, no mouse events are triggered as you scroll over each item, and an ACTION_EVENT occurs only when a specific choice is selected.

With the 1.1 event model, you register ItemListener with addItemListener(). Then when the user selects the Choice, the ItemListener.itemStateChanged() method is called through the protected Choice.processItemEvent() method. Key, mouse, and focus listeners are registered through the Component methods of addKeyListener(), addMouseListener(), and addFocusListener(), respectively. Action

public boolean action (Event e, Object o) {
 if (e.target instanceof Choice) {
 System.out.println ("Choice is now set to " + o);
}
return false;
}
Keyboard

The keyboard events for a Choice can be generated once the Choice has the input focus. In addition to the KEY_ACTION and KEY_ACTION_RELEASE events you get with the arrow keys, an ACTION_EVENT is generated over each entry.

Mouse

Ordinarily, the Choice component does not trigger any mouse events. Focus

Ordinarily, the Choice component does not trigger any focus events. Listeners and 1.1 event handling

With the 1.1 event model, you register listeners for different event types; the listeners are told when the event happens. These methods register listeners, and let the Choice component inspect its own events.

The following simple applet below demonstrates how a component can receive its own events by overriding processItemEvent(), while still allowing other objects to register as listeners. MyChoice11 is a subclass of Choice that processes its own item events. choice11 is an applet that uses the MyChoice11 component and registers itself as a listener for item events.

// Java 1.1 only import java.awt.*; import java.applet.*; import java.awt.event.*; class MyChoice11 extends Choice {
 MyChoice11 () {
 super (); enableEvents (AWTEvent.ITEM_EVENT_MASK);
}
protected void processItemEvent(ItemEvent e) {
 ItemSelectable ie = e.getItemSelectable(); System.out.println ("Item Selected: " + ie.getSelectedObjects()[0]); // If you do not call super.processItemEvent() // no listener will be notified super.processItemEvent (e);
}
}
public class choice11 extends Applet implements ItemListener {
 Choice c;
public void init () {
 String []fonts; fonts = Toolkit.getDefaultToolkit().getFontList(); c = new MyChoice11();
for (int i = 0; i < fonts.length; i++) {
 c.add (fonts[i]);
}
add (c); c.addItemListener (this);
}
public void itemStateChanged(ItemEvent e) {
 ItemSelectable ie = e.getItemSelectable(); System.out.println ("State Change: " + ie.getSelectedObjects()[0]);
}
} 

A few things are worth noticing. MyChoice11 calls enableEvents() in its constructor to make sure that item events are delivered, even if nobody registers as a listener: MyChoice11 needs to make sure that it receives events, even in the absence of listeners. Its processItemEvent() method ends by calling the superclass's processItemEvent() method, with the original item event. This call ensures that normal item event processing occurs; super.processItemEvent() is responsible for distributing the event to any registered listeners. The alternative would be to implement the whole registration and event distribution mechanism inside myChoice11, which is precisely what object-oriented developing is supposed to avoid, or being absolutely sure that you will only use MyChoice11 in situations in which there won't be any listeners, drastically limiting the usefulness of this class.

choice11 doesn't contain many surprises. It implements ItemListener, the listener interface for item events; provides the required itemStateChanged() method, which is called whenever an item event occurs; and calls MyChoice11's method addItemListener() to register as a listener for item events. (MyChoice11 inherits this method from the Choice class.)