Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Tab Key and KeyListeners

 
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
 
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


 
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
 
Slideshow boring ... losing consciousness ... just gonna take a quick nap on this tiny ad ...
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic