Toolbars3.java

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


/**
Demonstrate toolbars with icons!
*/
class Toolbars3 extends JFrame
{
    // Just plop the main() method right in the frame class...
    public static void main(String argv[])
    {
        Toolbars3 f = new Toolbars3();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.show();
    }


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

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

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

        // Create a Color submenu.
        JMenu optionsMenu = new JMenu("Colors");
        fileMenu.addSeparator();
        fileMenu.add(optionsMenu);

        // Create a toolbar.
        JToolBar tb = new JToolBar("Colors");
        contentPane.add(tb,BorderLayout.NORTH);

        // Define a list of colors and names.
        class Bozo {
            Color c;
            String name;
            ImageIcon icon;
            Bozo(Color c,String name,String iname)
            {
                this.c = c; 
                this.name = name;
                this.icon = new ImageIcon(iname);
            }
            }
        Bozo[] clist = {
            new Bozo(Color.WHITE,"White","ball_white.gif"),
            new Bozo(Color.RED,"Red","ball_red.gif"),
            new Bozo(Color.ORANGE,"Orange","ball_orange.gif"),
            new Bozo(Color.GREEN,"Green","ball_green.gif"),
            new Bozo(Color.BLUE,"Blue","ball_blue.gif"),
            new Bozo(Color.MAGENTA,"Magenta","ball_purple.gif"),
            };

        // Create an Action object for each color, 
        // and add it to buttons, menus, and toolbar.
        for (int i=0; i<clist.length; i++) {
            ColorAction action = 
                new ColorAction(sp,
                    clist[i].c,clist[i].name,clist[i].icon);
            JButton button = new JButton(action);
            sp.add(button);
            JMenuItem option = new JMenuItem(action);
            optionsMenu.add(option);
            tb.add(action); // Can also add a JButton.
            }

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


/**
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,ImageIcon icon)
    {
        super(name,icon);
        this.color = c;
        this.panel = p;
    }
    public void actionPerformed(ActionEvent e)
    {
        panel.setBackground(color);
    }
    private JPanel panel;
    private Color color;
}