import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ComboBoxDemo2 extends JPanel {
    static JFrame frame;

    JLabel picture;

    public ComboBoxDemo2() {
	String[] petStrings = { "Bird",
				"Cat",
				"Dog",
				"Rabbit",
				"Teddy" };

	// Create the combo box, turn off editing, and select the first one
	JComboBox petList = new JComboBox(petStrings);
	petList.setSelectedIndex(0);

        // Register a listener for the combo box
	ComboListener myListener = new ComboListener();
	petList.addActionListener(myListener);

	picture = new JLabel(new ImageIcon("images/" + petStrings[0] + ".gif"));
	picture.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));

        // The preferred size is hard-coded to be the width of the
        // widest image and the height of the tallest image + the border.
        // A real program would compute this.
	picture.setPreferredSize(new Dimension(177, 122+10));

	setLayout(new BorderLayout());
	add(petList, "North");
	add(picture, "Center");
	setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
    }


    /** Listens to the combo box. */
    class ComboListener implements ActionListener { 
        public void actionPerformed(ActionEvent e) {
	    JComboBox cb = (JComboBox)e.getSource();
	    picture.setIcon(new ImageIcon("images/" +
					   (String)cb.getSelectedItem() +
					   ".gif"));
        }
    }

    public static void main(String s[]) {
         WindowListener l = new WindowAdapter() {
             public void windowClosing(WindowEvent e) {System.exit(0);}
         };
 
         frame = new JFrame("ComboBoxDemo2");
         frame.addWindowListener(l);
         frame.getContentPane().add("Center", new ComboBoxDemo2());
         frame.pack();
         frame.setVisible(true);
    }
}
