tags:

views:

1123

answers:

5

I have few TextFields on my frame. I want to know the TextField name which is currently focused. Can someone please help me? How can I find out the current focused object name?

Thanks

+1  A: 

This tutorial should be pretty helpful to understand focus.

Mitch Flax
A: 

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.

Uri
Seems really inefficient. You can just call KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
Joe Attardi
+4  A: 

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

Rob
+1  A: 

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.

euphoria83
+2  A: 

Also have a look at the javax.swing.FocusManager

Carlos Heuberger