Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Append a text on Cursorposition

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am coding a textEditor with the awt (not with swing) and i want to insert a text on actual Cursorposition. With int nu=editor.getCaretPosition(); i will get the actual Linenumber - but HOW DO I APPEND THE TEXT ON THIS POSITION???
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at the documentation for TextArea we find the method insert(String, int). For the int parameter, use getCaretPosition().
Here's an example:

Was there a hidden message in that?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by da funky chicken:
With ... getCaretPosition() ... i will get the actual Linenumber

That's not correct. With getCaretPosition() you get the caret position within the TextArea (or whatever TextComponent) with regards to the beginning of the String that the TextArea contains. That String begins at the top left corner. The top left corner has a position of 0. If "Hello," were at the beginning of the TextArea, and the caret were between the two 'l' characters, then getCaretPosition() would return 3 - not the line number.
Good Luck.
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
da funky chicken
Welcome to the Java Ranch, we hope you’ll enjoy visiting as a regular however,
your name is not in keeping with our naming policy here at the ranch. Please change your display name to an appropriate name as shown in the policy.
Thanks again and we hope to see you around the ranch!!
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx for help, dirk. It worx!!!
[ March 28, 2002: Message edited by: da funky chicken ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic