This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Set focus to a component.  RSS feed

 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How to set the focus to a component in Swing. I used the requestFocus() it's not working. Also i have heard that it produces undesiarable result's.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is from the api, and could be the explanation of your 'undesirable results'

"public void requestFocus()
...
Because the focus behavior of this method is platform-dependent, developers
are strongly encouraged to use requestFocusInWindow when possible."

as for it not working, the component must be visible prior to calling requestFocus()

i.e. the end of your GUI constructor would look something like this

...
panel.add(textField)
getContentPane().add(panel};
setVisible(true);
textField.requestFocusInWindow();
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!