Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextField select text and caret position  RSS feed

 
Jiri Nejedly
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
Is it possible to have selection in JTextField and cursor position at the beginning of this selection?

I tried this

textField.setCaretPosition(start);
textField.select(start, end);

- it moves cursor at the end of the selection.

And this

textField.select(start, end);
textField.setCaretPosition(start);

sets the cursor right, but clears the selection.

Am I doing something wrong? Thanks








 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the API for select(...) inherited from JTextComponent and you'll see that it's a legacy method provided for backward compatibility.

You'll also find the preferred way to manage selection, which will provide a solution to your problem.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!