    01: import java.awt.*;
    02: import javax.swing.*;
    03:
    04: /**
    05:    This program demonstrates action objects. Two actions
    06:    insert greetings into a text area. Each action can be
    07:    triggered by a menu item or toolbar button. When an
    08:    action is carried out, the opposite action becomes enabled.
    09: */
    10: public class CommandTester
    11: {
    12:    public static void main(String[] args)
    13:    {
    14:       JFrame frame = new JFrame();
    15:       JMenuBar bar = new JMenuBar();
    16:       frame.setJMenuBar(bar);
    17:       JMenu menu = new JMenu("Say");
    18:       bar.add(menu);
    19:       JToolBar toolBar = new JToolBar();
    20:       frame.add(toolBar, BorderLayout.NORTH);
    21:       JTextArea textArea = new JTextArea(10, 40);
    22:       frame.add(textArea, BorderLayout.CENTER);
    23:
    24:       GreetingAction helloAction = new GreetingAction(
    25:             "Hello, World", textArea);
    26:       helloAction.putValue(Action.NAME, "Hello");
    27:       helloAction.putValue(Action.SMALL_ICON,
    28:          new ImageIcon("hello.png"));
    29:
    30:       GreetingAction goodbyeAction = new GreetingAction(
    31:             "Goodbye, World", textArea);
    32:       goodbyeAction.putValue(Action.NAME, "Goodbye");
    33:       goodbyeAction.putValue(Action.SMALL_ICON,
    34:             new ImageIcon("goodbye.png"));
    35:
    36:       helloAction.setOpposite(goodbyeAction);
    37:       goodbyeAction.setOpposite(helloAction);
    38:       goodbyeAction.setEnabled(false);
    39:
    40:       menu.add(helloAction);
    41:       menu.add(goodbyeAction);
    42:
    43:       toolBar.add(helloAction);
    44:       toolBar.add(goodbyeAction);
    45:
    46:       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    47:       frame.pack();
    48:       frame.setVisible(true);
    49:    }
    50: }