Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out which object currently has focus

Tags:

java

focus

swing

I have a few TextFields in my Frame. I want to know which TextField currently has focus. How can I find this information?

like image 255
Talha Bin Shakir Avatar asked Apr 06 '09 03:04

Talha Bin Shakir


2 Answers

KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() 
like image 180
Peter Tseng Avatar answered Sep 30 '22 15:09

Peter Tseng


JFrame.getFocusOwner() (inherited from Window.getFocusOwner()) ought to return a reference to the component with focus. getMostRecentFocusOwner() might also be of interest.

like image 26
Rob Avatar answered Sep 30 '22 16:09

Rob