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

- 2,792
- 1
- 21
- 28

- 2,563
- 10
- 38
- 54
8 Answers
KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()

- 13,613
- 4
- 67
- 57
-
3Much better than the accepted answer, IMHO, since I can do this from a static class that does not have access to the window I'm actually using, and I don't have to loop through all windows. – Ky - Jul 31 '15 at 13:08
JFrame.getFocusOwner()
(inherited from Window.getFocusOwner()
) ought to return a reference to the component with focus. getMostRecentFocusOwner()
might also be of interest.

- 47,999
- 5
- 74
- 91
You could also listen for the appropriate property change in keyboard focus manager:
KeyboardFocusManager.getCurrentKeyboardFocusManager().addPropertyChangeListener("focusOwner", new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println(evt.getNewValue());
}
});
This outputs focus owner as you interact with Swing components and is useful for debugging focus issues in general.

- 5,528
- 32
- 60
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.

- 14,768
- 17
- 63
- 73
KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()
;
wont work across threads. So if your app invokes a new thread and that thread has its own frame/window etc then it wont be able to gain the focus owner from that thread. Instead use: KeyboardFocusManager.getCurrentKeyboardFocusManager().getGlobalFocusOwner();
-
okay, reading the api doc helps (me :-) to understand what you might mean. It states the _Returns the focus owner, even if the calling thread is in a different context than the focus owner_ - it's not usable in application code, though, because it's scope is protected. It's used internally only ... – kleopatra Jul 25 '13 at 09:03
-
3Swing is a single threaded environment, all UI interactions MUST occur from within the context of the Event Dispatching Thread, it is impossible to have two windows within the same JVM operating in different threads, that's not how the API works – MadProgrammer Jan 27 '15 at 23:08
You can get currently focused Component like that:
Component focusOwner = FocusManager.getCurrentManager().getFocusOwner();
After that You can check if focusOwner
is instance of TextField

- 3,887
- 6
- 36
- 59
Every JComponent has a hasFocus method that you can use to check if it has focus. However, this has been changed, and now you should use isFocusOwner.
So run over all the text fields in your frame, and check on each of them if it is isFocusOwner by calling that method.
You could also get the focus owner through the frame.

- 88,451
- 51
- 221
- 321
-
8Seems really inefficient. You can just call KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); – Joe Attardi Jan 14 '10 at 17:08