Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTextField Action on Tab key

 
Chris Shepherd
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to cause my JTextField to generate an ActionEvent when I hit Tab just as it does when I hit Enter. I searched for it, and didn't see an apparent solution in the results I looked at(but I didn't read all 166 of them).
Is there an easy way to do this that I just missed? It seems like the JTextField is not even ever seeing the tab events at all. I subclassed JTextField and watched the processKeyEvent() method and it never even hit that method when the tab key was pressed. Is the JPanel it's in eating that keypress as part of the focus traversal process? Is there any way force that keypress to show itself to the JTextField?

Thanks for any help,
Chris
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using 1.4 or later, try this:

textField.setFocusTraversalKeysEnabled(false)
 
Chris Shepherd
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beautiful!!

Now that I can see it, I can work out some way to trigger on it.

Thanks for your help,
Chris
 
Chris Shepherd
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, bonus for anyone looking in here to find a solution. here's how I ended up making this work best. I should have thought of this before, but hey..

David gave me good input, but it ended up being easier to simply set up a focus listener on the JTextField and when tab took the focus away, I did my business with the JTextField content. This makes it work when the mouse clicks away to somewhere else as well. To make this work with the Enter keypress, I used an actionlistener that just called myJTextField.transferFocus(). This makes the Enter keypress act like Tab, moving focus to the next component and in the process activating my focus listener which takes care of the JTextField content.

Hope this helps anyone here with questions,

Thanks again David,
Chris
 
Will Barbee
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an application with multiple JTextField's each with its own data to be processed and edited.
I have the application working using the Enter key and ActionListener.

I would like it to work with the Tab key as well as the Enter key. Thus when the user entered data and pressed the Tab key,
the application would process the data and move to the next field, just as the Enter key Action Listener code does now.

The information in this post is just what I need.
Thank you.
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out how key bindings work.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic