File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TAB KeyStroke

 
Nick George
Ranch Hand
Posts: 815
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Craig Wood
Ranch Hand
Posts: 1535
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 262
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 815
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. For what it's worth, it's a good old fashioned JComponent.
 
Will Barbee
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Mike.
Will do.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic