/* * File: ListDynTest.java * * Create a scrolling List with multiple selections, using ListSelectionEvents * for (singly) selected items. Shows how the user can add and * remove items (with TextFields). * * Copyright: Northeast Parallel Architectures Center * */ import javax.swing.*; import javax.swing.JComponent.*; import java.awt.Color; import java.awt.Toolkit; import java.awt.Font; import javax.swing.event.*; import java.util.*; import java.lang.*; import java.awt.TextField; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class ListDynTest extends JApplet implements ListSelectionListener, ActionListener { // Allow five visible items in the list, // and allow multiple selections: private JList wordList ; private JScrollPane scrollPane; private String resultstring; private JLabel resultlabel; private JLabel newlabel = new JLabel("Add an adjective: "); private JTextField newitemtext = new JTextField (20); private JLabel oldlabel = new JLabel(" Remove one: "); private JTextField olditemtext = new JTextField (20); private Font f = new Font ( "Dialog", Font.PLAIN, 18 ); String words[] = {"Snowy","Cold","Icy","Cloudy","Windy" ,"Horrible","Damp" ,"Extra Snowy","Insalubrious" }; Vector vWords = new Vector(); public void init() { setBackground( Color.white ); setFont (f); // Color the scrolling list: Color purple = new Color( 127, 0, 255); //store the string[] in a vector for (int i= 0; i< words.length; i++) vWords.addElement(words[i]); wordList = new JList(vWords); scrollPane = new JScrollPane(wordList); wordList.setBackground( purple ); wordList.setForeground( Color.white ); wordList.setVisibleRowCount(3); // Construct a text label: resultstring = "Welcome to winters in Syracuse!"; resultlabel = new JLabel( resultstring); JPanel p = new JPanel(); JPanel addtext = new JPanel(); JPanel remtext = new JPanel(); p.add(scrollPane); addtext.add(newlabel); addtext.add(newitemtext); remtext.add(oldlabel); remtext.add(olditemtext); getContentPane().add(p,"North"); getContentPane().add(resultlabel,"South"); getContentPane().add (addtext,"West"); getContentPane().add (remtext,"East"); // Register the applet to listen for events: newitemtext.addActionListener(this); olditemtext.addActionListener(this); wordList.addListSelectionListener( this ); } public void valueChanged(ListSelectionEvent event) { updateLabel(); } public void actionPerformed( ActionEvent event ) { // if a textfield has been entered, determine which one: //add to list if (event.getSource() == newitemtext) { vWords.addElement(newitemtext.getText()); newitemtext.setText (" "); } //remove from list if (event.getSource() == olditemtext) { if(vWords.removeElement(olditemtext.getText()) == false) olditemtext.setText ("No such item to remove!"); else olditemtext.setText (" "); } //refresh list wordList.setListData(vWords); wordList.setSelectedIndex(vWords.size()-1); updateLabel(); } public void updateLabel() { // get the String array of the labels of selected items Object[] itemobj = wordList.getSelectedValues(); String itemlabels[]=new String[itemobj.length]; for (int i=0;i