Title: Message
Enjoy.
 
Mystifier
 
/*
 * Created on Mar 18, 2005
 * Added another pair of Text Fields
 *
 */
package mystifier.examples.keypress;
 
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
 
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
 
/**
 * @author mystifier
 *
 */
public class KeyPressExample extends JPanel  implements KeyListener {
 private JTextField textField1;
 private JTextField textField2;
 private JTextField textField3;
 private JTextField textField4;
 
  public KeyPressExample() {
        super(new GridBagLayout());
 
        textField1 = new JTextField(20);
        textField1.addKeyListener(this);
       
        textField2 = new JTextField(20);
 
        textField3 = new JTextField(20);
        textField3.addKeyListener(this);
       
        textField4 = new JTextField(20);
        //Add Components to this panel.
        GridBagConstraints c = new GridBagConstraints();
 
        add("TextFiled 1 : ", textField1, c);     
        add("TextFiled 2 : ", textField2, c);
        add("TextFiled 3 : ", textField3, c);
        add("TextFiled 4 : ", textField4, c);
 
    }
 
 /**
  *
  */
 private void add(String labelText,JTextField textField,GridBagConstraints c) {
        add(new JLabel(labelText));
        c.gridwidth = GridBagConstraints.REMAINDER;
        add(textField, c);
 }
 
 //The three events
 public void keyTyped(KeyEvent e) {}
 
 public void keyPressed(KeyEvent e) {}
 
 public void keyReleased(KeyEvent e) {
  if(e.getComponent() == this.textField1)
   textField2.setText(textField1.getText());
  else if(e.getComponent() == this.textField3)
   textField4.setText(textField3.getText());
 }
 
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(true);
 
        //Create and set up the window.
        JFrame frame = new JFrame("TextDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        //Create and set up the content pane.
        JComponent newContentPane = new KeyPressExample();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);
 
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Chetan Pandey
Sent: Wednesday, March 23, 2005 4:55 PM
To: Indian Java Users Group at Delhi
Subject: RE: [JUG-Delhi] Two Text Fields

Dear Mystifier:
 
You code works for me. Thanks a lot.
But I have this minor difficulty.
The thing is I have two pair of Textfields(tf1,tf4) and (tf2,tf5).
 
When something is typed in tf1, it should update it to tf4 and the same in case of tf2 and tf5.
 
But since I have only one method for keypressed(KeyEvent e), how do I tell keyPressed to distinguish between key pressed events in tf1 and tf2.
 
Pls check code below:
 tf1 = new JTextField(10);
  tf1.addKeyListener(this);
  tf1.setActionCommand("tf1"); // I know this aint right
  
  tf2 = new JTextField(10);
  tf2.addKeyListener(this);
  tf2.setActionCommand("tf2");  // I know this aint right
.
.
.
 
public void keyReleased(KeyEvent e) { 
 String str1;     
if( (e.getActionCommand()).equals("tf1") )    // what instead of e.getActionCommand() so that   // it will recognize the correct text field 
{
 str1 = (tf1.getText().toString()).trim();
 tf4.setText(str1 );
}
 
else if( (e.getActionCommand()).equals("tf4") )   
{
 str1 = (tf2.getText().toString()).trim();
  tf5.setText(str1);
}  

}
 
Thanks a lot again.
 
Chetan
_______________________________________________
Java mailing list
[email protected]
http://mail.jug-delhi.org/mailman/listinfo/java_jug-delhi.org

Reply via email to