//: c13:ChoiceNew.java // Drop-down lists (combo boxes). // <applet code=ChoiceNew // width=450 height=175></applet> import javax.swing.*; import java.awt.*; import java.awt.event.*; import com.bruceeckel.swing.*; 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); } } 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.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.addWindowListener(new WClose()); // 1.2 frame.add(applet); frame.setSize(450,200); applet.init(); applet.start(); frame.setVisible(true); } } ///:~