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

Focus on a JTextField?  RSS feed

 
Joel Cochran
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have several JTextFields in an dialog. Sometimes I can populate the first field, and when I do I want to automatically move the cursor to the next entry field, but I can't seem to find a method to focus or place the cursor in a JTextField.
I also tried adding \t to the end of my field string, but it doesn't actually tab to the next entry field.
Any ideas?
TIA,
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Component::requestFocus()
or even better
Component::requestFocusInWindow()
Good Luck.
 
Joel Cochran
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alas, neither solution appears to work. I'm surprised there isn't a positionCursor() or focusCurso() method or something like that for JTextComponents...
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Component::setCaretPosition(int)
This won't bring focus to a component - it will just set the caret position.
Note: The previous suggestions need to be called at the proper time (they work well for me). See the API documentation.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!