• 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

doubt in focus

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic