1 /* create a scrolling list */ 2 3 import java.awt.*; 4 5 public class ListTest extends java.applet.Applet { 6 7 private Label label; 8 // Allow five visible items in the list and 9 // disallow multiple selections: 10 private List list = new List(5, false); 11 12 public void init() { 13 setBackground(Color.white); 14 15 list.addItem("Hamlet"); 16 list.addItem("Claudius"); 17 list.addItem("Gertrude"); 18 list.addItem("Polonius"); 19 list.addItem("Horatio"); 20 list.addItem("Laertes"); 21 list.addItem("Ophelia"); 22 list.addItem("Caesar"); 23 list.addItem("Brutus"); 24 list.addItem("Alexandrius"); 25 list.setBackground(Color.white); 26 27 setLayout(new BorderLayout(100,40)); 28 add("Center", list); 29 label = new Label("", Label.CENTER); 30 label.setBackground(Color.white); 31 add("South", label); 32 // Add blank labels for spacing: 33 add("North", new Label("")); 34 add("East", new Label("")); 35 add("West", new Label("")); 36 } 37 38 // Double-clicking an item triggers an action event: 39 public boolean action(Event event, Object arg) { 40 if ( event.target instanceof List ) { 41 // Handling List event: 42 if ( event.target == list ) { 43 String s = "You chose "; 44 s += list.getSelectedItem() + "!"; 45 label.setText(s); 46 } else { 47 return super.action(event, arg); 48 } 49 return true; 50 } 51 return super.action(event, arg); 52 } 53 54 }