File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Tab Key and KeyListeners Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Tab Key and KeyListeners" Watch "Tab Key and KeyListeners" New topic
Author

Tab Key and KeyListeners

John Palmieri
Greenhorn

Joined: Jul 05, 2001
Posts: 8
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

Joined: Dec 01, 2000
Posts: 255
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


There's nothing more dangerous than a resourceful idiot. ---Dilbert
SAFROLE YUTANI
Ranch Hand

Joined: Jul 06, 2001
Posts: 257
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tab Key and KeyListeners
 
Similar Threads
use of tab key in table
Acheiving Focus using EnterKey
JTextField and TAB key
Turning off Tab traversal in a JTextArea
Pressing a key from the code