• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Tab Key and KeyListeners

 
John Palmieri
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am trying to write a KeyListener that can detect when the tab key is pressed within a JTextField. The Listener detects all keys accept the tab key. It seems that some lower level method is consuming the key before it get to my listener.
My application needs to put up a dialog if the tabs out of the text field.
Any help would be appreciated.
John Palmieri
 
Matt Senecal
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post some code? I've never had a problem getting a KeyListener to catch the TAB key.
Originally posted by John Palmieri:
Hello,
I am trying to write a KeyListener that can detect when the tab key is pressed within a JTextField. The Listener detects all keys accept the tab key. It seems that some lower level method is consuming the key before it get to my listener.
My application needs to put up a dialog if the tabs out of the text field.
Any help would be appreciated.
John Palmieri

 
SAFROLE YUTANI
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there is a lower-level method that dispatches, not consumes, all events generated from a component and sends the events to the appropriate listener for that component.
You need to subclass JTextField and override processKeyEvent(KeyEvent key). Any type of key event generated from this component, this method will dispatch the event to all of this components key listeners, so there must be some special processing that blocks the TAB key, no problem thought, since we can override the method and perform our own processing, and then call super.processKeyEvent(KeyEvent) to handle all other key events other than TAB.
I just tested this code to catch the TAB key and it works, good luck.

class CustomTextField extends JTextField {
public CustomTextField( int c){
super(c);
}
protected void processKeyEvent(KeyEvent ke){
if( ke.getKeyCode() == KeyEvent.VK_TAB)
System.out.println("TAB");
//you must call this!!
super.processKeyEvent( ke);
}
}
SAF
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic