Meaningless Drivel is fun!
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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Making control have initial focus" Watch "Making control have initial focus" New topic

Making control have initial focus

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1129
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:
But nothing works.
Any ideas?
-- Mike
Raj K

Joined: Sep 10, 2002
Posts: 1
Try this:
this.addWindowListener(new java.awt.event.WindowAdapter()
public void windowOpened(WindowEvent e)
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: 1129
Yes, using WindowAdaptor worked.
Thank you!!!
-- Mike
I agree. Here's the link:
subject: Making control have initial focus
It's not a secret anymore!