• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

catching TAB in key event

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am trying to catch a tab event between JTextFields in order to send the focus to a specific place (which changes). I have tried:

if (e.getKeyCode() == KeyEvent.VK_TAB) {
....

in keyPressed and in keyReleased, but they don't register a TAB event.

Any ideas?

Alice
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It has been a long time since I worked with Swing so this may be off the wall. Perhaps you should be looking for a FocusEvent instead of a key event - that is my reading of the java.awt.event.FocusEvent javadocs.
Bill
 
Alice B Palmer
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've tried it with the lostFocus, but it can't differentiate between a tab and a mouse click.

What I have at the moment is that when the JTextField looses focus it moves to a specific point in the form. If you tab through the fields it works fine, but if you click...

Let me try to explain a different way.

For the sake of argument let's say I have four JTextFields t1, t2, t3, and t4. When I tab around these fields I want to go t1, t2, t3, t1, etc and skip t4. The problem is when I'm in t3 (which on lostFocus tells it to go to t1) and I mouse click in t2, it takes me to t1 (and does what it's been told to do).

I hope I've explained this ok.
 
Marshal
Posts: 28245
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JComponent has a method "setFocusTraversalPolicy" which sounds like it does what you want. ("Sets the focus traversal policy that will manage keyboard traversal of this Container's children...") Haven't used it myself though.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know if that will help you or not but if you declare you t4 to be non focusable:
t4.setFocuable(false);
it will never receive focus.
 
Alice B Palmer
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul,
I will try the setFocusTraversalPolicy, which I've never used either, and see how it goes.

samir,
Thank you for your suggestion, but I do want to be able to click in t4 with the mouse, but never get there with the tab key, so setFocusable(false) wouldn't work for me.
 
samir Sadiki
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alice,
I hope you figured out the answer to your question.
I am posting this code just in case, I think it solves your problem.
 
Alice B Palmer
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
samir,

Thank you, this is exactly the sort of thing I was looking for! I haven't had a chance to look at my program this week. I might get some time next week so I'll let you know how I got on.
 
reply
    Bookmark Topic Watch Topic
  • New Topic