rsyntaxtextarea retrieving text on event

Questions on using RText should go here.

Moderator: robert

rsyntaxtextarea retrieving text on event

Postby latusaki » Fri Nov 25, 2011 5:09 pm

How is it possible to retrieve the text written when some event that i specify(lets say when the line changes) occurs ? And how can I specify that event ? Thanks !
latusaki
 

Re: rsyntaxtextarea retrieving text on event

Postby robert » Mon Nov 28, 2011 3:04 am

This kind of functionality is inherited from the standard Swing JTextComponent class. You're most certainly looking for DocumentListener, but should also look at CaretListener. See these articles for a good overview:

Using Text Components
How to Write a Document Listener

A more general discussion of event listeners in Swing can be found in the article Writing Event Listeners.

Hope this helps!
User avatar
robert
 
Posts: 774
Joined: Sat May 10, 2008 5:16 pm

Re: rsyntaxtextarea retrieving text on event

Postby latusaki » Fri Dec 02, 2011 12:50 am

Hello and thanks for your response !
I have one more question. Is it possible to check on which line/row I'm writing on and also control on which line/row some string is going to be inserted? I checked the documentation for JTextArea , RTextArea and Rsyntaxtextarea and I couldn't find something other then getRows().

Thanks !
latusaki
 

Re: rsyntaxtextarea retrieving text on event

Postby robert » Sun Dec 04, 2011 6:55 am

Sure, here are some methods to look at:

  • getCaretLineNumber(): Line number the caret is on
  • getLineOfOffset(int offs): Pass in an offset (such as from a DocumentEvent), get back the line it's in
  • getLineStartOffset(int offs): Get the starting offset of the line containing a given offset
  • getLineEndOffset(int offs): Get the ending offset of the line containing a given offset

If you want to intercept user edits, and do things such as forbid them from editing text on certain lines (or redirect their insertions to the last line, for example), you will want to use the DocumentFilter class. Create a subclass that does what you want, and install it on the RSTA instance's Document (cast to RSyntaxDocument to see the setDocumentFilter() method, since it's defined on AbstractDocument but not in the Document interface).
User avatar
robert
 
Posts: 774
Joined: Sat May 10, 2008 5:16 pm

Re: rsyntaxtextarea retrieving text on event

Postby latusaki » Sun Dec 04, 2011 3:42 pm

Thanks a lot Robert ! The DocumentFilter was exactly what I needed this was really helpful !
latusaki
 

Re: rsyntaxtextarea retrieving text on event

Postby latusaki » Fri Feb 03, 2012 9:45 pm

Hey robert got one final question.
I am copying in real time code written on a RSyntaxTextArea from a client , to another RSyntaxTextArea on a different machine over network. So when one types some text the other will see it shortly after.
Although when a user opens brackets and closing bracket is generated, on the other side the closing bracket is not generated. Is there some way I can make sure that the closing bracket will get generated ? Or maybe force the text area to close all open brackets ?(although thats not ideal ether)

I could remove bracket generation which partially solves the problem although i prefer to keep the feature if possible.
latusaki
 

Re: rsyntaxtextarea retrieving text on event

Postby robert » Sun Feb 05, 2012 5:49 am

How are you getting the changes in one RSyntaxTextArea to send to the next? I believe if you get them via a DocumentListener, you should be able to get everything, including stuff auto-inserted such as matching curly braces. The important thing is to listen to the Document, not the text component itself (extending and overriding replaceSelection(), etc.), as all edits must go through the Document.
User avatar
robert
 
Posts: 774
Joined: Sat May 10, 2008 5:16 pm


Return to Help

Who is online

Users browsing this forum: No registered users and 1 guest