//: 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 Chip",
"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;
}
} ///:~
//: List2.java
// Using lists with handleEvent()
import java.awt.*;
import java.applet.*;
public class List2 extends Applet {
String[] flavors = { "Chocolate", "Strawberry",
"Vanilla Fudge Swirl", "Mint Chip",
"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 handleEvent(Event evt) {
if(evt.id == Event.LIST_SELECT ||
evt.id == Event.LIST_DESELECT) {
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
return super.handleEvent(evt);
}
else
return super.handleEvent(evt);
return true;
}
public boolean action(Event evt, Object arg) {
if(evt.target.equals(b)) {
if(count < flavors.length)
lst.addItem(flavors[count++], 0);
}
else
return super.action(evt, arg);
return true;
}
} ///:~