1 /* 2 * File: ListDynTest.java 3 * 4 * Create a scrolling List with multiple selections, using ItemEvents 5 * for (singly) selected items. Shows how the user can add and 6 * remove items (with TextFields). 7 * 8 * Copyright: Northeast Parallel Architectures Center 9 * 10 */ 11 12 import java.awt.List; 13 import java.awt.Label; 14 import java.awt.Color; 15 import java.awt.Font; 16 import java.awt.TextField; 17 import java.awt.event.ItemListener; 18 import java.awt.event.ActionListener; 19 import java.awt.event.ItemEvent; 20 import java.awt.event.ActionEvent; 21 22 public class ListDynTest extends java.applet.Applet 23 implements ItemListener, ActionListener 24 { 25 // Allow five visible items in the list, 26 // and allow multiple selections: 27 private List wordList = new List( 5, true ); 28 29 private String resultstring; 30 private Label resultlabel; 31 private Label newlabel = new Label("Add an adjective: "); 32 private TextField newitemtext = new TextField (20); 33 private Label oldlabel = new Label(" Remove one: "); 34 private TextField olditemtext = new TextField (20); 35 private Font f = new Font ( "Dialog", Font.PLAIN, 18 ); 36 37 public void init() { 38 setBackground( Color.white ); 39 setFont (f); 40 41 // Color the scrolling list: 42 Color purple = new Color( 127, 0, 255); 43 wordList.setBackground( purple ); 44 wordList.setForeground( Color.white ); 45 46 47 // Add items to the scrolling list: 48 wordList.addItem( "Snowy" ); 49 wordList.addItem( "Cold" ); 50 wordList.addItem( "Icy" ); 51 wordList.addItem( "Cloudy" ); 52 wordList.addItem( "Windy" ); 53 wordList.addItem( "Horrible" ); 54 wordList.addItem( "Damp" ); 55 wordList.addItem( "Extra Snowy" ); 56 wordList.addItem( "Insalubrious" ); 57 58 // Register the applet to listen for events: 59 wordList.addItemListener( this ); 60 61 // Construct a text label: 62 resultstring = " Welcome to winters in Syracuse! "; 63 resultlabel = new Label( resultstring, Label.CENTER ); 64 65 newitemtext.addActionListener(this); 66 olditemtext.addActionListener(this); 67 68 add( wordList ); 69 add( resultlabel ); 70 add ( newlabel ); 71 add ( newitemtext ); 72 add ( oldlabel ); 73 add ( olditemtext ); 74 75 } 76 77 public void itemStateChanged( ItemEvent event ) 78 { // if an item has either been selected or deselected 79 updateLabel(); 80 } 81 82 public void actionPerformed( ActionEvent event ) 83 { // if a textfield has been entered, determine which one: 84 if (event.getSource() == newitemtext) 85 wordList.add ( newitemtext.getText()); 86 if (event.getSource() == olditemtext) 87 try { wordList.remove ( olditemtext.getText()); } 88 catch (IllegalArgumentException ex) 89 { olditemtext.setText ("No such item to remove!"); } 90 updateLabel(); 91 } 92 93 94 95 public void updateLabel() 96 { 97 // get the String array of the labels of selected items 98 String itemlabels[] = wordList.getSelectedItems(); 99 100 // Build string of comma-separated items: 101 String str = "Welcome to "; 102 for ( int i = 0; i < itemlabels.length; i++ ) 103 { 104 str += itemlabels[i] + ","; 105 } 106 // Insert "and" in the appropriate place: 107 if ( !str.equals( "" ) ) { 108 str += " winters in Syracuse!"; 109 int pos = str.lastIndexOf( "," ); 110 if ( pos != -1 ) 111 str = str.substring( 0, pos ) + str.substring( pos + 1 ); 112 } 113 resultlabel.setText( str ); // update label 114 } 115 116 } 117 118