• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Tab Key and KeyListeners

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic