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

        // Display a JComboBox that allows the background color
        // of the central panel to be selected.
class SwingComboFrame extends JFrame {
    public SwingComboFrame() {
        final int width = 175, height = 250;
        setSize(width,height);
        Container contents = getContentPane();

        setupComboBox();
        contents.add(getBox(),"North");
        contents.add(getPanel(),"Center");
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(new QuitJButton());
        contents.add(buttonPanel,"South");
    }

        // Add colored labels to the combo box.
    protected void setupComboBox(){
        // Color/Name pairs.
        final Color[] colors = {
            Color.red, Color.orange, Color.yellow, Color.green,
            Color.cyan, Color.blue, Color.magenta, Color.white,
        };

        // Add SquareIcon elements to the box's model.
        final JComboBox box = getBox();
        final DefaultComboBoxModel model = (DefaultComboBoxModel) box.getModel();
        final int numColors = colors.length;
        for(int i = 0; i < numColors; i++){
            model.addElement(new SquareIcon(colors[i]));
        }

        // Act on selections.
        box.addItemListener(new ItemListener(){
            public void itemStateChanged(ItemEvent e){
                if(e.getStateChange() == ItemEvent.SELECTED){
                    SquareIcon icon = (SquareIcon) box.getSelectedItem();
                    // Change the central panel's background color.
                    getPanel().setBackground(icon.getColor());
                }
            }
        });

        // The user should not be able to enter a new item.
        box.setEditable(false);
    }

    protected JComboBox getBox(){
        return box;
    }

    protected JPanel getPanel(){
        return panel;
    }

    // A color choosing combo box.
    private final JComboBox box = new JComboBox(new DefaultComboBoxModel());
    // A central panel to be colored.
    private final JPanel panel = new JPanel();
}
