• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTextField select text and caret position

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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








 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic