It's not a secret anymore!*
The moose likes Swing / AWT / SWT and the fly likes Making control have initial focus Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Making control have initial focus" Watch "Making control have initial focus" New topic
Author

Making control have initial focus

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1064
I don't see how to see how to set the initial focus of a JTextField in a JFrame after the JFrame loads.
I want to have focus already be inside the first text field.
There should be an easy way to set the initial focus, but I haven't found it. This would be analogous to setting the "Tab-Order" in Microsoft products.
I've tried things like:
jTextField.setFocusCycleRoot(true);
TextField.requestFocus();
But nothing works.
Any ideas?
Thanks.
-- Mike
Raj K
Greenhorn

Joined: Sep 10, 2002
Posts: 1
Try this:
this.addWindowListener(new java.awt.event.WindowAdapter()
{
public void windowOpened(WindowEvent e)
{
your_textfield.requestFocus();
}
}
Dorothy Finkel-Laverty
Ranch Hand

Joined: Nov 24, 2001
Posts: 51
The trick to it is to requestFocus() AFTER you have setVisible to true, not before. It seems to be negated if you setVisible to true after you request focus.
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1064
Yes, using WindowAdaptor worked.
Thank you!!!
-- Mike
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Making control have initial focus