import java.awt.*;
import java.awt.event.*;
public class SearchableListApplet extends java.applet.Applet {
public void init() {
String [] items = { "One", "Two", "Three", "Four", "Five", "Six" };
add( new SearchableList( items ) );
}
}
class SearchableList extends Container implements ActionListener {
List list;
TextField field;
SearchableList( String [] items ) {
list = new List( 3 ); // Let some scroll for this example
for(int i=0; i< items.length; i++)
list.addItem( items[i] );
field = new TextField();
field.addActionListener( this );
setLayout( new BorderLayout() );
add("Center", list);
add("South", field);
}
private void actionPerformed( ActionEvent e ) {
String search = field.getText();
for (int i=0; i< list.getItemCount(); i++)
if ( list.getItem( i ).equals( search ) ) {
list.select( i );
list.makeVisible( i ); // Scroll it into view
break;
}
field.setText("");
}
}
/* Notes
Two relevant listener interfaces...
ItemListener for single clicks to select an item
ActionListener for double clicks on an item
This example doesn't do anything with the events from the list...
What should it do?
*/
|