This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes to get default focus Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "to get default focus " Watch "to get default focus " New topic

to get default focus

shilpa patil

Joined: Jan 29, 2006
Posts: 6
hello all,
I am having a window with a textField.
I want whenever this window is shown , focus should be on this textfield
How can I do this.
Thanks in advance. :roll:
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
textfield.requestFocusInWindow() is the method, but if you are talking about
when the program first opens, the method must be called after the program's
visibility is set to true.

if you are using 1.5, there have been some changes to the focus subsystem that
causes the above method not to work properly. in this case try wrapping the
method in a SwingUtilities.invokeLater(), or, as a last resort, use a timer
with repeats set to false.
shilpa patil

Joined: Jan 29, 2006
Posts: 6
it worked
Thanks again for the prompt reply
It is sorta covered in the JavaRanch Style Guide.
subject: to get default focus
jQuery in Action, 3rd edition