The
list box is completely different from the selection box, not just when we activate the selection box, and the list box is fixed at the specified location on the screen. In addition, a list box allows multiple selections: if we click on more than one item, the selection is high brightness and we can choose as many as we want. If we want to see a list of items, we can call GetSelectedItem () to produce a list of selected items. To delete an item from a group, we have to click it again. list box, of course there's a problem with the default action is to double-click instead of click. Click to add or remove items from the group, and double-click Invoke action (). The solution to this problem is to retrain our users as the following program assumes.
: List1.java//Using lists with action () import java.awt.*;
Import java.applet.*; public class List1 extends Applet {string[] flavors = {"Chocolate", "Strawberry", "Vanilla Fudge, Swirl", "Mint Chi
P "," Mocha Almond Fudge "," Rum Raisin "," Praline Cream "," Mud Pie "};
Show 6 items, allow multiple selection:list lst = new List (6, true);
TextArea t = new TextArea (flavors.length, 30);
Button B = New button ("Test");
int count = 0;
public void init () {t.seteditable (false);
for (int i = 0; i < 4; i++) Lst.additem (flavors[count++]);
Add (t);
Add (LST);
Add (b);
Public boolean action (Event evt, Object Arg) {if (Evt.target.equals (LST)) {T.settext ("");
string[] items = Lst.getselecteditems ();
for (int i = 0; i < items.length i++) T.appendtext (items[i] + "\ n");
else if (Evt.target.equals (b)) {if (Count < Flavors.length) Lst.additem (flavors[count++), 0);
} else Return super.action (EVT, ARG);
return true; }
} ///:~
When the button is pressed, the button increases the item to the top of the list (because the second argument for AddItem () is 0). Adding items to a list box is more reasonable than a selection box because the user expects to scroll through a list box (for that reason, it has built-in scroll bars) but the user does not want to have to figure out how to scroll to the desired item, as in the previous example.
However, the only way to invoke action () is by double-clicking. If we want to monitor what users do in our list (especially clicks), we must provide an alternative.