/* ComboBoxDemo.java        Authors: Koffman & Wolz
 * Application demostrating use of a combo box.
 * Uses Swing, AWT.
 */
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class ComboBoxDemo extends JFrame
                          implements ItemListener {

  private JComboBox titleList;
  private JTextField greeting = new JTextField(20);

  // Creates a GUI with a combo box for selecting a title
  //   and a text field for output.
  public ComboBoxDemo() {
    getContentPane().setLayout(new FlowLayout());
    String titles[] = {"Mr.", "Ms.", "Mrs.", "Dr."};
    titleList = new JComboBox(titles);
    titleList.addItem("Professor");
    getContentPane().add(titleList);
    getContentPane().add(greeting);
    titleList.addItemListener(this);
  }

  // postcondition: Displays a greeting that includes the title
  //   selected from the combo box.
  public void itemStateChanged(ItemEvent itEv) {
    if (itEv.getSource() == titleList) {
      String titleStr = (String) titleList.getSelectedItem();
      greeting.setText("Dear " + titleStr + " Jones;");
    }
  }

  // Creates the frame and closes the frame.
  public static void main(String[] args) {
    ComboBoxDemo cBD = new ComboBoxDemo();
    cBD.setSize(400, 200);
    cBD.setVisible(true);
    cBD.setTitle("Combo box demo");
    cBD.addWindowListener(
        new WindowAdapter() {
           public void windowClosing(WindowEvent e) {
              System.exit(0);
           }
        }
    );
  }
}


