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 CommandTest
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:       Container contentPane = frame.getContentPane();
21:       contentPane.add(toolBar, BorderLayout.NORTH);
22:       JTextArea textArea = new JTextArea(10, 40);
23:       contentPane.add(textArea, BorderLayout.CENTER);
24: 
25:       GreetingAction helloAction = new GreetingAction(
26:          "Hello, World", textArea);
27:       helloAction.putValue(Action.NAME, "Hello");
28:       helloAction.putValue(Action.SMALL_ICON, 
29:          new ImageIcon("hello.png"));
30: 
31:       GreetingAction goodbyeAction = new GreetingAction(
32:          "Goodbye, World", textArea);
33:       goodbyeAction.putValue(Action.NAME, "Goodbye");
34:       goodbyeAction.putValue(Action.SMALL_ICON, 
35:          new ImageIcon("goodbye.png"));
36: 
37:       helloAction.setOpposite(goodbyeAction);
38:       goodbyeAction.setOpposite(helloAction);
39:       goodbyeAction.setEnabled(false);
40: 
41:       menu.add(helloAction);
42:       menu.add(goodbyeAction);
43: 
44:       toolBar.add(helloAction);
45:       toolBar.add(goodbyeAction);
46: 
47:       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
48:       frame.pack();
49:       frame.show();      
50:    }
51: }