Source of CommandTester.java


  1: import java.awt.*;
  2: import javax.swing.*;

  4: /**
  5:    This program demonstrates action objects. Two actions
  6:    insert greetings into a text area. Each action can be
  7:    triggered by a menu item or toolbar button. When an
  8:    action is carried out, the opposite action becomes enabled.
  9: */
 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);

 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"));

 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"));

 36:       helloAction.setOpposite(goodbyeAction);
 37:       goodbyeAction.setOpposite(helloAction);
 38:       goodbyeAction.setEnabled(false);

 40:       menu.add(helloAction);
 41:       menu.add(goodbyeAction);

 43:       toolBar.add(helloAction);
 44:       toolBar.add(goodbyeAction);

 46:       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 47:       frame.pack();
 48:       frame.setVisible(true);
 49:    }
 50: }