I have a few TextField
s in my Frame
. I want to know which TextField
currently has focus. How can I find this information?
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.
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.
Also have a look at the javax.swing.FocusManager