Two Laptop Bag
The moose likes Beginning Java and the fly likes Using KeyListener on multiple TextFields Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using KeyListener on multiple TextFields" Watch "Using KeyListener on multiple TextFields" New topic

Using KeyListener on multiple TextFields

Bryan Hyde

Joined: Jul 14, 2000
Posts: 1
I am trying to use KeyListener on multiple TextFields in one applet. How do I ensure that the applet knows which one generated the event and modifies the other TextFields accordingly?
Paul Caudle
Ranch Hand

Joined: Jun 08, 2000
Posts: 64
Hi Bryan, I hope this is the type of answer you're looking for here:
the KeyEvent class inherits a method called getSource() which will return the object that dispatched the event in the first place. What you could do would be something like:
if(keyEventObject.getSource() == TextFieldObject){
Do whatever you want if the source was that textField;
This will work well if you're handling your event in the same class that you declared your TextFields, which is usually what one would do.
Hope that helps, Bryan,
I agree. Here's the link:
subject: Using KeyListener on multiple TextFields
jQuery in Action, 3rd edition