Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Making control have initial focus

 
Mike London
Ranch Hand
Posts: 1158
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:
this.addWindowListener(new java.awt.event.WindowAdapter()
{
public void windowOpened(WindowEvent e)
{
your_textfield.requestFocus();
}
}
 
Dorothy Finkel-Laverty
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, using WindowAdaptor worked.
Thank you!!!
-- Mike
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic