/* * File: ChoiceTest.java * * Choosing items from a drop-down menu. Shows type Choice and how * to add items. Shows ItemEvents caused by selecting an item on a menu. * * Copyright: Northeast Parallel Architectures Center * */ import java.awt.Color; import java.awt.Font; import java.awt.Label; import java.awt.Choice; import java.awt.event.ItemListener; import java.awt.event.ItemEvent; public class ChoiceTest extends java.applet.Applet implements ItemListener { private Label label1, label2, label3; private Choice directorylist, formatlist; private Font f = new Font ("Dialog", Font.PLAIN, 24 ); public void init() { setBackground( Color.white ); setFont (f); label1 = new Label( "Directory: ", Label.RIGHT ); directorylist = new Choice(); directorylist.addItem( "HTML" ); directorylist.addItem( "JavaScript" ); directorylist.addItem( "CGI" ); directorylist.addItem( "Java" ); directorylist.addItem( "JDBC" ); directorylist.addItem( "VRML" ); // Select "JavaScript" as the default menu choice: directorylist.select(1); // directorylist.setBackground( Color.white ); label2 = new Label( " Format: ", Label.RIGHT ); formatlist = new Choice(); formatlist.addItem( ".zip" ); formatlist.addItem( ".tar.Z" ); formatlist.addItem( ".tar.gz" ); formatlist.addItem( ".sit" ); // Select ".tar.gz" as the default menu choice: formatlist.select(2); // formatlist.setBackground( Color.white ); label3 = new Label(); label3.setAlignment( Label.CENTER ); updateLabel(); add( label1 ); add( directorylist ); add( label2 ); add( formatlist ); add( label3 ); directorylist.addItemListener( this ); formatlist.addItemListener( this ); } public void itemStateChanged( ItemEvent event ) { // if an item is changed on either choice list, update the label updateLabel(); } public void updateLabel() { String s = directorylist.getSelectedItem(); s += formatlist.getSelectedItem(); label3.setText( "File: " + s ); } }