//: c13:ChoiceNew.java // From Thinking in Java, 2nd Edition // Available at http://www.BruceEckel.com // (c) Bruce Eckel 1999 // Copyright notice in Copyright.txt // Drop-down lists (combo boxes) // import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ChoiceNew extends JApplet { String[] descriptions1 = { "Ebullient", "Obtuse", "Recalcitrant", "Brilliant" }; String[] descriptions2 = { "Somnescent", "Timorous", "Florid", "Putrescent" }; JTextArea t = new JTextArea(7, 40); JComboBox c = new JComboBox(descriptions1); JButton b = new JButton("Add items"); int count = 0; public void init() { Container cp = getContentPane(); cp.setLayout(new FlowLayout()); t.setLineWrap(true); t.setEditable(false); cp.add(t); cp.add(c); cp.add(b); c.addItemListener(new CL()); b.addActionListener(new BL()); } class CL implements ItemListener { public void itemStateChanged(ItemEvent e) { t.setText("index: " + c.getSelectedIndex() + " " + e.toString()); } } class BL implements ActionListener { public void actionPerformed(ActionEvent e) { if(count < descriptions2.length) c.addItem(descriptions2[count++]); if(count >=descriptions2.length) b.setEnabled(false); } } public static void main(String[] args) { JApplet applet = new ChoiceNew(); Frame frame = new Frame("ChoiceNew"); frame.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e){ System.exit(0); } }); frame.add(applet); frame.setSize(450,200); applet.init(); applet.start(); frame.setVisible(true); } } ///:~