0

i have two text fields(tf1 and tf2) on which in have used KeyEvent to get the typed characters.

JTextField tf1 = new JTextField(10);
        JTextField tf2 = new JTextField(10);
        tf1.setFocusable(true);
        tf2.setFocusable(true);
        //regerstring for event
        tf1.addKeyListener(new KeyHandler(tf1, tf2));
        tf2.addKeyListener(new KeyHandler(tf1, tf2));








 class KeyHandler extends KeyAdapter{
    JTextField tf1;
    JTextField tf2;
    KeyHandler(JTextField tf1, JTextField tf2){
    tf1 = this.tf1;
    tf2 = this.tf2;
    }
    public void keyTyped(KeyEvent e){
    char ch = e.getKeyChar();
    System.out.println(e.getKeyLocation());

    if(e.getSource() == tf1)
        System.out.println("tf1");
    else if (e.getSource() == tf2)
    System.out.println("tf2");

    }

i have tried getSource() of KeyEvent class but it returns the object of JTextField , i has to diffrentiate between tf1 and tf2.

How can i get associated textfiled reference in keyTyped()

navyad
  • 3,752
  • 7
  • 47
  • 88

4 Answers4

5

It's difficult to be 100% sure, but it would appear you have a scope issue. You key handler can't see your fields.

public void someMethod() {
    JTextField tf1 = new JTextField(10);
    JTextField tf2 = new JTextField(10);

    KeyHandler handler = new KeyHandler();
    tf1.addKeyListener(handler);
    tf2.addKeyListener(handler);
}

public class KeyHandler extends KeyAdapter{
    public void keyTyped(KeyEvent e){
        // Error, tf1 is unknown...
        if (e.getSource() == tf1) {...}
    }
}

If you want to be able to compare which field you have, you have two options. Declare the fields as instance fields or identify the fields via their name property.

Option 1

public class SomeClass extends ... {
    private JTextField tf1;
    private JTextField tf2;
    public void someMethod() {
        JTextField tf1 = new JTextField(10);
        JTextField tf2 = new JTextField(10);

        KeyHandler handler = new KeyHandler();
        tf1.addKeyListener(handler);
        tf2.addKeyListener(handler);
    }

    public class KeyHandler extends KeyAdapter{
        public void keyTyped(KeyEvent e){
            // tf1 is now within scope :D
            if (e.getSource() == tf1) {...}
        }
    }
 }

Option 2

public void someMethod() {
    JTextField tf1 = new JTextField(10);
    tf1.setName("tf1");
    JTextField tf2 = new JTextField(10);
    tf2.setName("tf2");

    KeyHandler handler = new KeyHandler();
    tf1.addKeyListener(handler);
    tf2.addKeyListener(handler);
}

public class KeyHandler extends KeyAdapter{
    public void keyTyped(KeyEvent e){
        Object source = e.getSource();
        if (source instanceof JTextField) {
            JTextField field = (JTextField)source;
            String name = field.getName();
            if ("tf1".equals(name)) {
                // Hello TextField #1
            }
        }
    }
}

Disclaimer

Now, I have no idea why you want to do what you want to do, but KeyListeners are not the most suitable option for filtering or monitoring changes to text fields. For one, you have no guarantee in what order your listener will be called in, the fields Document may or may not be updated before the listener is fired. Secondly, they are not fired if the user pastes text into the field.

A better choice would be to use a DocumentListener, which can be used to monitor changes to the fields Document or a DocumentFilter, which can be used to filter content being sent to the document.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • -100 for reinvent the wheel, and wrong way (I know very well that you love KeyListener, maybe AWTEvent is better way, required only focus for Container), +1 for `Disclaimer` – mKorbel Oct 24 '12 at 10:29
  • @mkorbel actually I don't, I prefer document listeners/filters & key bindings ... Thanks in a large part to you, the question is vague & without context. My intention was to try a lead the OP to water and then offer them a ice cream ;) – MadProgrammer Oct 24 '12 at 10:32
  • @mkorbel did I mention I agree with you – MadProgrammer Oct 24 '12 at 11:12
4
mKorbel
  • 109,525
  • 20
  • 134
  • 319
2

Compare the addresses as follows :

if (e.getSource() == tf1) { 
   // Source is the first text field
}

As MadProgrammer pointed out use instance variables for textfields you need to use outside the creation scope (in your case tf1 and tf2)

giorashc
  • 13,691
  • 3
  • 35
  • 71
  • 1
    From the OP, it would suggest that tf1 & tf2 are not instance variables, but are declared locally, you may want to point that out ;) – MadProgrammer Oct 24 '12 at 10:00
  • Dorothy, I'm afraid we are no longer in Javascript. You can't just access the variables in the outer scope :-) – John Dvorak Oct 24 '12 at 10:03
0
public void keyTyped(KeyEvent e) {
    // retries the typed character
    char ch = e.getKeyChar();
    System.out.println(ch);
    if (e.getComponent().getName().equals("TF1")) {
        // Key Typed in Text Field 1
    } else if (e.getComponent().getName().equals("TF2")) {
        // Key Typed in Text Field 2
    }
    // associated textfield of typed char
}

Set The Name of individual text Boxes using tf1.setName("TF1")

yash ahuja
  • 470
  • 1
  • 8
  • 23