How to find out which object currently has focus

getFocusOwner() will return the child component which is currently focused.

But you have to check to see if it is a JTextField. Other components like buttons might be focused if they exist in your frame as well.


KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()

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

Tags:

Java

Swing

Focus