GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes JTextField Action on Tab key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextField Action on Tab key" Watch "JTextField Action on Tab key" New topic
Author

JTextField Action on Tab key

Chris Shepherd
Ranch Hand

Joined: Jun 27, 2000
Posts: 286
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

Joined: Jul 27, 2001
Posts: 1365
If you're using 1.4 or later, try this:

textField.setFocusTraversalKeysEnabled(false)
Chris Shepherd
Ranch Hand

Joined: Jun 27, 2000
Posts: 286
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

Joined: Jun 27, 2000
Posts: 286
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

Joined: Oct 01, 2008
Posts: 41
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

Check out how key bindings work.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
Don't get me started about those stupid light bulbs.
 
subject: JTextField Action on Tab key