/* * File: ListDynTest.java * * Create a scrolling List with multiple selections, using ItemEvents * for (singly) selected items. Shows how the user can add and * remove items (with TextFields). * * Copyright: Northeast Parallel Architectures Center * */ import java.awt.List; import java.awt.Label; import java.awt.Color; import java.awt.Font; import java.awt.TextField; import java.awt.event.ItemListener; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ActionEvent; public class ListDynTest extends java.applet.Applet implements ItemListener, ActionListener { // Allow five visible items in the list, // and allow multiple selections: private List wordList = new List( 5, true ); private String resultstring; private Label resultlabel; private Label newlabel = new Label("Add an adjective: "); private TextField newitemtext = new TextField (20); private Label oldlabel = new Label(" Remove one: "); private TextField olditemtext = new TextField (20); private Font f = new Font ( "Dialog", Font.PLAIN, 18 ); public void init() { setBackground( Color.white ); setFont (f); // Color the scrolling list: Color purple = new Color( 127, 0, 255); wordList.setBackground( purple ); wordList.setForeground( Color.white ); // Add items to the scrolling list: wordList.addItem( "Snowy" ); wordList.addItem( "Cold" ); wordList.addItem( "Icy" ); wordList.addItem( "Cloudy" ); wordList.addItem( "Windy" ); wordList.addItem( "Horrible" ); wordList.addItem( "Damp" ); wordList.addItem( "Extra Snowy" ); wordList.addItem( "Insalubrious" ); // Register the applet to listen for events: wordList.addItemListener( this ); // Construct a text label: resultstring = " Welcome to winters in Syracuse! "; resultlabel = new Label( resultstring, Label.CENTER ); newitemtext.addActionListener(this); olditemtext.addActionListener(this); add( wordList ); add( resultlabel ); add ( newlabel ); add ( newitemtext ); add ( oldlabel ); add ( olditemtext ); } public void itemStateChanged( ItemEvent event ) { // if an item has either been selected or deselected updateLabel(); } public void actionPerformed( ActionEvent event ) { // if a textfield has been entered, determine which one: if (event.getSource() == newitemtext) wordList.add ( newitemtext.getText()); if (event.getSource() == olditemtext) try { wordList.remove ( olditemtext.getText()); } catch (IllegalArgumentException ex) { olditemtext.setText ("No such item to remove!"); } updateLabel(); } public void updateLabel() { // get the String array of the labels of selected items String itemlabels[] = wordList.getSelectedItems(); // Build string of comma-separated items: String str = "Welcome to "; for ( int i = 0; i < itemlabels.length; i++ ) { str += itemlabels[i] + ","; } // Insert "and" in the appropriate place: if ( !str.equals( "" ) ) { str += " winters in Syracuse!"; int pos = str.lastIndexOf( "," ); if ( pos != -1 ) str = str.substring( 0, pos ) + str.substring( pos + 1 ); } resultlabel.setText( str ); // update label } }