More then setText() and getText()

General Discussion on RSyntaxTextArea.

Moderator: robert

More then setText() and getText()

Postby theuserbl » Sun Sep 26, 2010 5:06 pm

Hi!

Actually I know only about getText and setText for manipulating the text in a RSyntaxTestArea.
But it would be nice, to change only one line/row.
Additional it would be nice, if I change the line (with [Enter], the Arrow-Keys or so on),that I can then with a function/method reorganize/restructure/reformat the line before.

Greatings
theuserbl
theuserbl
 
Posts: 15
Joined: Sun Mar 28, 2010 2:37 pm

Re: More then setText() and getText()

Postby robert » Mon Sep 27, 2010 3:34 am

Actually I know only about getText and setText for manipulating the text in a RSyntaxTestArea.
But it would be nice, to change only one line/row.


The means to do this comes from the methods inherited from JTextArea:

Code: Select all
public int getLineStartOffset(int line)
public int getLineEndOffset(int line)
public void replaceRange(String str, int start, int end)
public void insert(String str, int pos)


You can simply get the starting and ending offsets of the line to change, and modify it as appropriate with replaceRange() and/or insert().

Additional it would be nice, if I change the line (with [Enter], the Arrow-Keys or so on),that I can then with a function/method reorganize/restructure/reformat the line before.


You can do this by adding a CaretListener to the editor. When the caret moves, you can use the getLineOfOffset(int offs) method to get the line the caret is on, and the methods above to then modify it as you wish. Of course, if you want to modify the *previous* line, you'll have to store the *previous* caret position in your CaretListener. Then, each time the caret position changes, get the line of the *previous* caret position, and operate on that line, and then update your "previous caret position" field. That way you're always modifying the line where the caret was, not where it's just moved to.

Good luck!
User avatar
robert
 
Posts: 801
Joined: Sat May 10, 2008 5:16 pm


Return to Open Discussion

Who is online

Users browsing this forum: No registered users and 4 guests

cron