Lists

Like the Choice component, the List provides a way to present your user with a fixed sequence of choices to select. However, with List, several items can be displayed at a time on the screen. A List can also allow multiple selection, so that more than one choice can be selected.

Normally, a scrollbar is associated with the List to enable the user to move to the items that do not fit on the screen. On some platforms, the List may not display the scrollbar if there is enough room to display all choices. A List can be resized by the LayoutManager according to the space available. Figure 9.2 shows two lists, one of which has no items to display.

List Methods

Constructors

Figure 9.2: Two lists; the list on the right is empty

[Graphic: Figure 9-2]Content control

NOTE:

Early versions ( Java1.0) of the clear() method did not work reliably across platforms. You were better off calling the method listVar.delItems(0, listVar.countItems()-1), where listVar is your List instance.

Selection and positioning

NOTE:

A negative index seems to select everything within the List. This seems more like an irregularity than a feature to rely upon.

Sizing Miscellaneous methods
java.awt.List[0,34,107x54,selected=null] 

List Events

The primary event for a List occurs when the user selects an item in the list. With the 1.0 event model, double-clicking a selection causes an ACTION_EVENT and triggers the action() method, while single-clicking causes a LIST_SELECT or LIST_DESELECT event. Once the List has the input focus, it is possible to change the selection by using the arrow or keyboard keys. The arrow keys scroll through the list of choices, triggering the KEY_ACTION, LIST_SELECT, LIST_DESELECT, and KEY_ACTION_RELEASE events, and thus the keyDown(), handleEvent(), and keyUp() methods (no specific method gets called for LIST_SELECT and LIST_DESELECT). action() is called only when the user double-clicks on an item with the mouse. If the mouse is used to scroll through the list, no mouse events are triggered; ACTION_EVENT is generated only when the user double-clicks on an item.

With the 1.1 event model, you register an ItemListener with addItemListener() or an ActionListener with the addActionListener() method. When the user selects the List, either the ItemListener.itemStateChanged() method or the ActionListener.actionPerformed() method is called through the protected List.processItemEvent() method or List.processActionEvent() method. Key, mouse, and focus listeners are registered through the three Component methods of addKeyListener(), addMouseListener(), and addFocusListener(), respectively. Action

import java.awt.*; import java.applet.*;
public class list3 extends Applet {
 List l;
public void init () {
 String fonts[]; fonts = Toolkit.getDefaultToolkit().getFontList(); l = new List(4, true);
for (int i = 0; i < fonts.length; i++) {
 l.addItem (fonts[i]);
}
setLayout (new BorderLayout (10, 10)); add ("North", new Label ("Pick Font Set")); add ("Center", l); add ("South", new Button ("Submit")); resize (preferredSize()); validate();
}
public boolean action (Event e, Object o) {
 if (e.target instanceof Button) {
 String chosen[] = l.getSelectedItems();
for (int i=0;i<chosen.length;i++) System.out.println (chosen[i]);
}
return false;
}
} 

Figure 9.3: Multiselect List

[Graphic: Figure 9-3]Keyboard

Ordinarily, List generates all the KEY events once it has the input focus. But the way it handles keyboard input differs slightly depending upon the selection mode of the list. Furthermore, each platform offers slightly different behavior, so code that depends on keyboard events in List is not portable. One strategy is to take advantage of the keyboard events when they are available but allow for another way of managing the list in case they are not.

Mouse

Ordinarily, the List component does not trigger any mouse events. Double-clicking the mouse over any element in the list generates an ACTION_EVENT. Single-clicking could result in either a LIST_SELECT or LIST_DESELECT, depending on the mode of the List and the current state of the item chosen. When the user changes the selection with the mouse, the ACTION_EVENT is posted only when an item is double-clicked. List

There is a special pair of events for lists: LIST_SELECT and LIST_DESELECT. No special method is called when these events are triggered. However, you can catch them in the handleEvent() method. If the List is in single-selection mode, a LIST_SELECT event is generated whenever the user selects one of the items in the List. In multiple-selection mode, you will get a LIST_SELECT event when an element gets selected and a LIST_DESELECT event when it is deselected. The following code shows how to use this event type.

public boolean handleEvent (Event e) {
 if (e.id == Event.LIST_SELECT) {
 System.out.println ("Selected item: " + e.arg);
return true;
}
else {
 return super.handleEvent (e);
}
} 
Focus

Normally, the List component does not reliably trigger any focus events. Listeners and 1.1 event handling

With the 1.1 event model, you register listeners, and they are told when the event happens.