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 KeyStroke 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 KeyStroke" Watch "TAB KeyStroke" New topic
Author

TAB KeyStroke

Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
This is the kind of question I hate to ask, because I know it's something stupid, but it calls up the violent aspects of my nature.

getInputMap().put( KeyStroke.getKeyStroke( "F3" ) , "talk" );

works.

getInputMap().put( KeyStroke.getKeyStroke( "TAB" ) , "talk" );

doesn't work. I'm stumped. I want to use tab! Googling KeyStroke TAB came up with several examples of people using it, so I know the string is tab. I know my tab key works. If there's any other information that would be useful, I can provide it, but I don't know what would be pertinent.


I've heard it takes forever to grow a woman from the ground
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Binding the tab key is more complex than the others. JTextComponents have a tab key binding for adding tabs in JTextArea nad other components by design. Containers use the tab key for keyboard navigation among child components. To successfully use a tab key binding you have to remove the default key bindings that may exist on other key maps for a component. How to Use Key Bindings
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
Craig, if Nick were working with a JTextComponent as you assume, he wouldn't be having this problem. His code does replace the existing TAB key binding with his own. But most other components use the TAB key for focus traversal, and that means the InputMaps never see the keystroke. You can disable keyboard focus traversal (or assign different keys to it) with the setFocusTraversalKeys method.

(Heh! I didn't think that link would work, but you get the idea.)
[ November 14, 2005: Message edited by: Alan Moore ]
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
Thanks a lot. For what it's worth, it's a good old fashioned JComponent.
Will Barbee
Ranch Hand

Joined: Oct 01, 2008
Posts: 41
I have an application with two JTextFields. The program was working using Action Listener and the Enter
key to update the data from the JTextField into the work fields for SQL update.

I wanted to use the tab key to exit from the field and update the data and also move to the next JTextField.

I read the Tutorial on Key binding. I thought it started well but quickly dived too deep and never gave a close
example to my problem. I have read this JavaRanch topic and also the referenced Java topic. The referenced Java topic is not
helpful. I understand the words but the usage part is missing.

Here are my code examples for one of the two JTextFields:



Changing the data in the JTextField and pressing Enter works.

Changing the data and pressing the tab key does not. The System.out.println does
not print when you press the Tab key. I changed the TAB key to
F3 like the first entry in this topic. And that did not work either.

So obviously I have a programming error.
What is the correct code please?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I wanted to use the tab key to exit from the field and update the data and also move to the next JTextField.

what if the user uses the mouse to navigate to the next component?

(and you should start your own topic, linking to this thread if necessary)
Will Barbee
Ranch Hand

Joined: Oct 01, 2008
Posts: 41
Thanks, Mike.
Will do.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TAB KeyStroke