import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/** DemoFocusEvents - similar to <code>DemoActionEvents.java</code>,
* except using focus events.<p>
*
* <code>FocusListener</code> defines two methods: <code>focusGained</code>,
* and <code>focusLost</code>.  These methods execute when
* a component registered to listen for focus events either gains or
* looses focus.  With most look-and-feel implementations,
* focus  changes occur automatically through one of two user interactions:
* pressing the TAB
* key or clicking the mouse pointer on a component
* different from the one that currently has focus.<p>
*
* The signature in the extended <code>JFrame</code> includes
* the clause "<code>implements ActionListener, FocusListener</code>".  Both
* listeners are implemented; however, most of the work
* is done in the <code>focusLost</code> method.  In fact, the
* <code>actionPerformed</code> method (which executes when the
* user presses the ENTER key) simply "transfers focus",
* deferring the task of processing the text field's data
* to the <code>focusLost</code> method.  Thus, pressing
* ENTER is added as a third user interaction to change focus.<p>
*
* The behaviour of this program is identical to
* <code>DemoActionEvents</code> except the message is sent to
* the console for all three user interactions for transfering
* focus: (i) pressing the ENTER key, (ii) pressing the TAB key,
* and (iii) clicking with the mouse pointer.<p>
*
* @see <a href="DemoFocusEvents.java">source code</a>
* @author Scott MacKenzie, 2003
*/
public class DemoFocusEvents
{
   public static void main(String[] args)
   {
      DemoFocusEventsFrame frame = new DemoFocusEventsFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setTitle("DemoFocusEvents");
      frame.pack();
      frame.show();
   }
}

class DemoFocusEventsFrame extends JFrame
implements ActionListener, FocusListener
{
   private JTextField pro;
   private JTextField con;

   // constructor

   public DemoFocusEventsFrame()
   {
      // ----------------------------------
      // construct and configure components
      // ----------------------------------

      pro = new JTextField(10);
      con = new JTextField(10);

      // -------------
      // add listeners
      // -------------

      pro.addActionListener(this);
      con.addActionListener(this);
      pro.addFocusListener(this);
      con.addFocusListener(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();
      source.transferFocus();
   }

   public void focusGained(FocusEvent fe) {}
   public void focusLost(FocusEvent fe)
   {
      JTextField source = (JTextField)fe.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());
   }
}

