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

/**
 * A minimal class to illustrate the different ways that 
 * icons can be attached to buttons, and also to show how
 * simple user configuration is possible.  Only session-long
 * changes are possible, and there is no way to save or restore
 * the results.  By Tom Hayes, for Summer 2001 CSPP 535 course.
 */

public class IconDemo extends JFrame implements ActionListener{

    private final int N_ICONS = 4;  // Number of icons.
    private final int N_BUTTONS = 4;  // Number of buttons.
    private ImageIcon[] icons = new ImageIcon[N_ICONS];
    private String[] butText = new String[N_BUTTONS];
    private JButton[] buttons = new JButton[N_BUTTONS];
    private JComboBox iconSelector, buttonSelector;

    public IconDemo() {
        Container cp = getContentPane();
        cp.setLayout(new BoxLayout(cp,BoxLayout.X_AXIS));
        for (int i=0; i<N_BUTTONS; i++) {
            butText[i] = ""+(i+1);
            buttons[i] = (new JButton(butText[i]));
        }
        for (int i=0; i<N_ICONS; i++) {
            icons[i] = (new ImageIcon("images/icon"+(i+1)+".gif"));
        }
        buttonSelector = new JComboBox((Object[])butText);
        buttonSelector.addActionListener(this);
        iconSelector = new JComboBox((Object[])icons);
        iconSelector.addActionListener(this);
        cp.add(buttonSelector);
        cp.add(iconSelector);
        for (int i=0; i<N_BUTTONS; i++) {
            cp.add(buttons[i]);
        }
        // Standard JFrame code:
        addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }});
        pack();
        setVisible(true);
    }
    
    static public void main(String[] args) {
        IconDemo f = new IconDemo();
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == iconSelector) {
            ImageIcon ic = (ImageIcon)iconSelector.getSelectedItem();
            JButton but = buttons[buttonSelector.getSelectedIndex()];
            but.setIcon(ic);
            pack();
        }
    }

}




