1  /*
  2   *  File:  ChoiceTest.java
  3   *
  4   *  Choosing items from a drop-down menu.  Shows type Choice and how
  5   *   to add items.  Shows ItemEvents caused by selecting an item on a menu.
  6   *
  7   *  Copyright:  Northeast Parallel Architectures Center
  8   *  
  9   */
 10  
 11  import java.awt.Color;
 12  import java.awt.Font;
 13  import java.awt.Label;
 14  import java.awt.Choice;
 15  import java.awt.event.ItemListener;
 16  import java.awt.event.ItemEvent;
 17  
 18  public class ChoiceTest extends java.applet.Applet 
 19                          implements ItemListener {
 20  
 21    private Label label1, label2, label3; 
 22    private Choice directorylist, formatlist;
 23    private Font f = new Font ("Dialog", Font.PLAIN, 24 );
 24  
 25    public void init() {
 26      setBackground( Color.white );
 27      setFont (f);
 28  
 29      label1 = new Label( "Directory:  ", Label.RIGHT );
 30      
 31      directorylist = new Choice();
 32      directorylist.addItem( "HTML"       );
 33      directorylist.addItem( "JavaScript" );
 34      directorylist.addItem( "CGI"        );
 35      directorylist.addItem( "Java"       );
 36      directorylist.addItem( "JDBC"       );
 37      directorylist.addItem( "VRML"       );
 38      // Select "JavaScript" as the default menu choice:
 39      directorylist.select(1);
 40      //    directorylist.setBackground( Color.white );
 41  
 42      label2 = new Label( "   Format:  ", Label.RIGHT );
 43      
 44      formatlist = new Choice();
 45      formatlist.addItem( ".zip"    );
 46      formatlist.addItem( ".tar.Z"  );
 47      formatlist.addItem( ".tar.gz" );
 48      formatlist.addItem( ".sit"    );
 49      // Select ".tar.gz" as the default menu choice:
 50      formatlist.select(2);
 51      //    formatlist.setBackground( Color.white );
 52      
 53      label3 = new Label();
 54      label3.setAlignment( Label.CENTER );
 55      updateLabel();
 56      
 57      add( label1 ); add( directorylist );
 58      add( label2 ); add( formatlist );
 59      add( label3 );
 60      
 61      directorylist.addItemListener( this );
 62      formatlist.addItemListener( this );
 63    }
 64    
 65    public void itemStateChanged( ItemEvent event ) {
 66    // if an item is changed on either choice list, update the label
 67      updateLabel();
 68    }
 69    
 70    public void updateLabel() {
 71      String s = directorylist.getSelectedItem();
 72      s += formatlist.getSelectedItem();
 73      label3.setText( "File:  " + s );
 74    }
 75  
 76  }