Actions3.java

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


/**
Action object demo, with enable/disable behavior.
*/
class Actions3 extends JFrame
{
    public static void main(String argv[])
    {
        Actions3 f = new Actions3();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.show();
    }


    public Actions3()
    {
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension ss = kit.getScreenSize();
        setSize(ss.width/4,2*ss.height/3);
        setLocation(ss.width/4,ss.height/4);
        setTitle("CISC370-011 -- Actions, enabled/disabled");

        JPanel sp = new JPanel();
        Container contentPane = getContentPane();
        contentPane.add(sp);

        // Create a File menu.
        JMenuBar mbar = new JMenuBar();
        setJMenuBar(mbar);
        JMenu fileMenu = new JMenu("File");
        mbar.add(fileMenu);

        // Create a Color menu.
        JMenu optionsMenu = new JMenu("Colors");
        mbar.add(optionsMenu);

        // Create a list of colors and names.
        class Bozo {
            Color c;
            String name;
            Bozo(Color c,String name)
                { this.c = c; this.name = name; }
            }
        Bozo[] clist = {
            new Bozo(Color.WHITE,"White"),
            new Bozo(Color.RED,"Red"),
            new Bozo(Color.ORANGE,"Orange"),
            new Bozo(Color.YELLOW,"Yellow"),
            new Bozo(Color.GREEN,"Green"),
            new Bozo(Color.CYAN,"Cyan"),
            new Bozo(Color.BLUE,"Blue"),
            new Bozo(Color.PINK,"Pink"),
            new Bozo(Color.MAGENTA,"Magenta"),
            new Bozo(Color.LIGHT_GRAY,"Light gray"),
            new Bozo(Color.GRAY,"Gray"),
            new Bozo(Color.DARK_GRAY,"Dark gray"),
            new Bozo(Color.BLACK,"Black"),
            };

        // Create an Action object for each color, and add the
        // Action to buttons and menus.
        alist = new ColorAction[clist.length];
        eflag = false;
        for (int i=0; i<clist.length; i++) {
            alist[i] = new ColorAction(sp,clist[i].c,clist[i].name);
            JButton button = new JButton(alist[i]);
            sp.add(button);
            JMenuItem option = new JMenuItem(alist[i]);
            optionsMenu.add(option);
            }

        // Add a disable/enable item to the File menu.
        JMenuItem disItem = new JMenuItem("Enable/disable");
        fileMenu.add(disItem);
        disItem.addActionListener(new ActionListener() {
                // Toggle 'enabled' status for some buttons.
                public void actionPerformed(ActionEvent e)
                {
                    for (int i=0; i<alist.length; i++) {
                        if (i % 2 == 0)
                            alist[i].setEnabled(eflag);
                        else
                            alist[i].setEnabled(!eflag);
                        }
                    eflag = !eflag;
                }
            });

        // Finally, add an Exit item to the File menu.
        fileMenu.addSeparator();
        JMenuItem exitItem = new JMenuItem("Exit");
        fileMenu.add(exitItem);
        exitItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                    { System.exit(0); }
            });
    }

    ColorAction[] alist = null;
    boolean eflag;
}


/**
This class represents an <code>Action</code> that, whenever triggered,
sets the background of a given JPanel to a specified color.
*/
class ColorAction extends AbstractAction
{
    public ColorAction(JPanel p,Color c,String name)
    {
        super(name);
        this.color = c;
        this.panel = p;
    }
    public void actionPerformed(ActionEvent e)
    {
        panel.setBackground(color);
    }
    private JPanel panel;
    private Color color;
}