import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/** DemoKeyEvents2 - similar to <code>DemoKeyEvents.java</code>, except using two
* <code>JTextFields</code>.<p>
*
* Since there are two text fields, it is necessary to register
* both to receive key events.  In the key listener methods,
* it is also necessary to determine which text field
* triggered the event.  This is accomplished by calling the
* <code>getSource</code> method on the <code>KeyEvent</code> object
* passed to the <code>keyTyped</code> method.<p>
*
* The <code>getSource</code> method is actually a method of the
* <code>EventObject</code> class -- the class at the top of
* Java's event class hierarchy.  Its return type is <code>Object</code>.
* <code>getSource</code> returns a reference to the object on which
* the event took place.  In this program that object is a
* <code>JTextField</code>, either the <code>pro</code> or
* <code>con</code> text field.  We determine which through
* a series of <code>if</code> statements.  In the output message,
* the source of the key event is identified
* with the prefix <code>Pro</code> or <code>Con</code>.<p>
*
* Screen snap:<p>
* <center><img src="DemoKeyEvents2-1.gif"></center><p>
*
* @see <a href="DemoKeyEvents2.java">source code</a>
* @author Scott MacKenzie, 2003
*/
public class DemoKeyEvents2
{
   public static void main(String[] args)
   {
      DemoKeyEvents2Frame frame = new DemoKeyEvents2Frame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setTitle("DemoKeyEvents2");
      frame.pack();
      frame.show();
   }
}

class DemoKeyEvents2Frame extends JFrame implements KeyListener
{
   private JTextField pro;
   private JTextField con;

   // constructor

   public DemoKeyEvents2Frame()
   {
      // ----------------------------------
      // construct and configure components
      // ----------------------------------

      pro = new JTextField(10);
      con = new JTextField(10);

      // -------------
      // add listeners
      // -------------

      pro.addKeyListener(this);
      con.addKeyListener(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 KeyListener methods (3)
   // ---------------------------------

   public void keyPressed(KeyEvent ke) {}   // do nothing
   public void keyReleased(KeyEvent ke) {}  // do nothing
   public void keyTyped(KeyEvent ke)
   {
      Object source = ke.getSource();

      // check if event occurred on pro component

      if (source == pro)
         System.out.println("Pro : " + ke.paramString());

      // check if event occurred on con component

      else if (source == con)
         System.out.println("Con : " + ke.paramString());
   }
}

