import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/** DemoActionEvents - similar to <code>DemoKeyEvents2.java</code>,
* except using
* <code>ActionListener</code> instead of <code>KeyListener</code>.
* <p>
*
* In most cases, it is not necessary to trap each keystroke when
* a user enters text in a <code>JTextField</code>.  It usually makes more
* sense to fire an event only when the user finishes
* entering text, signalled, for example, by pressing the ENTER key.<p>
*
* We could, of course, check for the ENTER key in the
* <code>keyPressed</code> method:<p>
*
* <pre>
*     if (ke.getKeyCode() != KeyEvent.VK_ENTER)
*        return;
* </pre>
*
* The code above exits immediately if any other key is pressed,
* but this is rather inefficient.
*
* The alternative, demonstrated here, is to register the
* <code>JTextField</code> to listen for <i>action events</i> (instead of
* key events).  The process is much the same.
* There are three differences:<p>
*
* <ul>
* <li>Define the extended <code>JFrame</code> with the clause
* <code>implements ActionListener</code> (instead of
* <code>implements KeyListener</code>).
*
* <li>Invoke <code>addActionListener</code>
* (instead of <code>addKeyListener</code>) on any <code>JTextField</code>
* that is to listen for action events.
*
* <li>Implement the <code>actionPerformed</code> method in
* <code>ActionListener</code> (instead of the three methods in
* <code>KeyListener</code>)
* </ul>
*
* The <code>actionPerformed</code> method is the only method in
* <code>ActionListener</code>.  It only fires for a
* <code>JTextField</code>
* object when the ENTER key is pressed.<p>
*
* In this demo, we process the text in each <code>JTextField</code> only
* when the ENTER key is pressed, sending a simple message to the console.
* There are a few other minor details to attend to, however.  It would be
* nice if the mouse pointer -- known as a <i>caret</i> when entering text --
* advances to the next <code>JTextField</code> when the ENTER key is
* pressed.  This is accomplished using the <code>transferFocus</code> method
* at the end of the <code>actionPerformed</code> method.<p>
*
* The <code>transferFocus</code> method is defined in the
* <code>Component</code> class.  We can invoke it on a <code>JTextField</code>
* object, but not on an <code>Object</code> object:
*
* <pre>
*     Object source = ae.getSource();
*     ...
*     source.transferFocus(); // WRONG!
* </pre>
*
* In this application,
* we <i>know</i>
* the source of the action event is a <code>JTextField</code>, so
* we can safely cast the object returned by
* <code>getSource</code> to a <code>JTextField</code> object:
* <p>
*
* <pre>
*     JTextField source = (JTextField)ae.getSource();
*     ...
*     source.transferFocus(); // OK!
* </pre>
*
* The code above explicitly directs the program to transfer focus
* to the next text field when the user presses the ENTER key.  Focus
* is also transferred, if the user presses the TAB key
* or clicks in the next text field with the mouse
* pointer.  However, in these cases no message is sent to the console
* because neither pressing the TAB key nor clicking with the
* mouse pointer fires an action event.  These actions are
* accommodated correctly in <code>DemoFocusEvents</code>.<p>
*
* In summary, pressing the
* ENTER key fires an action event.  In the <code>actionPerformed</code>
* method we process the text in the <code>JTextField</code> <i>and</i>
* transfer focus to the next <code>JTextField</code>.<p>
*
* Example invocation:<p>
*
* <pre>
*     java DemoActionEvents
* </pre>
*
* <center><img src="DemoActionEvents-1.gif"></center><p>
*
* @see <a href="DemoActionEvents.java">source code</a>
* @author Scott MacKenzie, 2003
*/
public class DemoActionEvents
{
   public static void main(String[] args)
   {
      DemoActionEventsFrame frame = new DemoActionEventsFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setTitle("DemoActionEvents");
      frame.pack();
      frame.show();
   }
}

class DemoActionEventsFrame extends JFrame implements ActionListener
{
   private JTextField pro;
   private JTextField con;

   // constructor

   public DemoActionEventsFrame()
   {
      // ----------------------------------
      // construct and configure components
      // ----------------------------------

      pro = new JTextField(10);
      con = new JTextField(10);

      // -------------
      // add listeners
      // -------------

      pro.addActionListener(this);
      con.addActionListener(this);

      // ------------------
      // arrange components
      // ------------------

      // add components to panels

      JPanel banner = new JPanel();
      banner.add(new JLabel("WHAT'S YOUR OPINION ON ANCHOIVES?"));

      JPanel proPanel = new JPanel();
      proPanel.add(new JLabel("Pro:"));
      proPanel.add(pro);
      
      JPanel conPanel = new JPanel();
      conPanel.add(new JLabel("Con:"));
      conPanel.add(con);

      // put panels in a content pane panel

      JPanel contentPane = new JPanel();
      contentPane.setLayout(new GridLayout(3, 1));
      contentPane.add(banner);
      contentPane.add(proPanel);
      contentPane.add(conPanel);
      
      // make panel this JFrame's content pane

      this.setContentPane(contentPane);
   }

   // --------------------------
   // implement listener methods
   // --------------------------

   public void actionPerformed(ActionEvent ae)
   {
      JTextField source = (JTextField)ae.getSource();

      // check if event occurred on pro component

      if (source == pro)
         System.out.println("Pro : " + pro.getText());

      // check if event occurred on con component

      else if (source == con)
         System.out.println("Con : " + con.getText());

      // transfer focus to next component

      source.transferFocus();
   }
}

