• Post Reply Bookmark Topic Watch Topic
  • New Topic

doubt in focus

 
Thyagarajan Ramanujam
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

A field can lost focus by many ways. For example, pressing TAB, pressing SHIFT+TAB, mouse click on other fields, etc

Is it possible to know how the focus of a Field is lost?

Thanks. Any help is appreciated.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thyagarajan Ramanujam:
hi,

A field can lost focus by many ways. For example, pressing TAB, pressing SHIFT+TAB, mouse click on other fields, etc

Is it possible to know how the focus of a Field is lost?

Thanks. Any help is appreciated.


Take a look at FocusListener#focusLost. I am not sure it will help you figure out how the focus was lost. But then why do you need to figure it out?
 
Thyagarajan Ramanujam
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Maneesh,

FocusListener#focusLost doesnot help as it does have method to say the type.


I need to execute a method when a field loses focus only when tab is pressed.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thyagarajan Ramanujam:


I need to execute a method when a field loses focus only when tab is pressed.


Well looks like you will have to use a combination of FocusListener and KeyListener then.
 
Suraj Chandran
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A really interesting topic.
But by design point of view you are not supposed to decide actions based on how focus changed.

Anyway...I guess there may be some solutions. I can't post code for reasons, but can give you some ideas...

If you want that only TAB should control focus. For this use the following API in Component setFocusTraversalKeys(). Use this API to set TAB for FORWARD_TRAVERSAL_KEYS and none for all other ID's.

Now if you want that mouse operations should not change focus, then you can do this(Not a good idea I know):
Override transferFocus() and transferFocusBackwards() and whenver it is called, set a flag. Now to your textfield add a focus listener. In its focusLost event check whether the flag is set. If so that means, the request came from KeyboardFocusManager/keyboard actions, reset the flag, and you can continue normal operations. But if the flag was not set then it means it might be a mouse operation....in which case you can regrab the focus on the textfield....

But as I said before the idea itesel is a fundamental design issue
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!