1  import java.awt.event.*;
  2  import javax.swing.*;
  3  
  4  /**
  5     This action places a greeting into a text field 
  6     and afterwards disables itself and enables its 
  7     opposite action.
  8  */
  9  public class GreetingAction extends AbstractAction
 10  {
 11     /**
 12        Constructs a greeting action.
 13        @param greeting the string to add to the text area
 14        @param textArea the text area to which to add the greeting
 15     */      
 16     public GreetingAction(String greeting, JTextArea textArea)
 17     {
 18        this.greeting = greeting;
 19        this.textArea = textArea;
 20     }
 21  
 22     /**
 23        Sets the opposite action.
 24        @param action the action to be enabled after this action was
 25        carried out
 26     */
 27     public void setOpposite(Action action)
 28     {
 29        oppositeAction = action;
 30     }
 31  
 32     public void actionPerformed(ActionEvent event)
 33     {
 34        textArea.append(greeting);
 35        textArea.append("\n");
 36        if (oppositeAction != null)
 37        {
 38           setEnabled(false);
 39           oppositeAction.setEnabled(true);
 40        }
 41     }
 42  
 43     private String greeting;
 44     private JTextArea textArea;
 45     private Action oppositeAction;
 46  }