File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

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,
David Weitzman
Ranch Hand

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

Chris Shepherd
Ranch Hand

Joined: Jun 27, 2000
Posts: 286

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

Thanks for your help,
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,
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

Joined: Oct 27, 2005
Posts: 20274

Check out how key bindings work.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: JTextField Action on Tab key
It's not a secret anymore!