// // another example of the Java AWT, showing use of Choice class. // see class definition and main method for details. // import java.awt.* ; import java.awt.event.* ; // // a ChoiceExample object is a CloseableFrame containing a Choice // and a Label. when the user makes a selection, the index of // the current selection is displayed in the Label. (we could // also display its text, but that's already on the screen in // the Choice itself, so for variety .... ) // class ChoiceExample extends CloseableFrame implements ItemListener { Choice c ; Label display ; ChoiceExample(String[] items) { super("ChoiceExample") ; c = new Choice() ; display = new Label("", Label.CENTER) ; setDisplay() ; for (int i = 0 ; i < items.length ; i++) c.add(items[i]) ; c.addItemListener(this) ; setLayout(new GridLayout(0, 1)) ; add(c) ; add(display) ; pack() ; show() ; } // invoked when user makes a selection. public void itemStateChanged(ItemEvent e) { setDisplay() ; } // displays index of current selection. private void setDisplay() { display.setText("selected item at index " + c.getSelectedIndex()) ; } // // ---- main --------------------------------------------------- // // command-line arguments are elements in Choice. // public static void main(String[] args) { if (args.length < 1) { System.out.println("Arguments are items") ; System.exit(-1) ; } ChoiceExample c = new ChoiceExample(args) ; } }