• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Highlighting Text in JTextPane is SUPER SLOW!!

 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

Please have a look at the following code. In there, I have made the code small as much as I can.

Form.java



KeywordConnector.java





JavaKeywords.java







Here, what happens is, when the user save the file as ".java", it starts highlighting the java keywords entered by the user.

But, do the following..

1.Copy and paste a code which is really big, something like 300 lines.
2.Save as .java

Now it will become slow, it takes some times to highlight the code. After that, type some keywords, you wont even be able to type, because it gets stuck. This only happens when the code is too big.

Please help me to get rid of this issue. Thanks
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With a quick glance through your code, this jumps out as being woefully inefficient:First off, it's creating a large number of String objects which shortly become eligible for GC; then, it splits on a single whitespace rather than multiple whitespace with multiline (see the docs for java.util.regex.Pattern and/or a regex tutorial).

Take a look at the methods of javax.swing.text.Utilities; I would expect that using those methods in conjunction with Document#getText(...) would be more efficient than your present approach. But you also need to rethink your design to examine only the part of the Document that has been changed -- probably in the DocumentFilter. Examining the entire text is always going to be slow for large files.
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. I really appreciate it.


Take a look at the methods of javax.swing.text.Utilities; I would expect that using those methods in conjunction with Document#getText(...) would be more efficient than your present approach


You mean we have to find the LAST word, INSERTED OR EDITED? I think that is a very good idea!! I worked on it, but I couldn't get the last word. How can I get it? Please help..
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to access the current row and the edited word, but it didn't work using the methods in Utilities, but it didn't work. Please help..
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can make an SSCCE that shows the specific problem and does nothing else, I might take a look. Provided that can be done in a short enough code.
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:If you can make an SSCCE that shows the specific problem and does nothing else, I might take a look. Provided that can be done in a short enough code.


Thanks a lot for the reply. OK, I have omitted most of my code, and reduced it to 163 lines. From line 123-126, you will see my failed attempt, in COMMENTS. Here is the code, it is changed, but it still uses the other class and interface which I have posted above, Below is my edited "Form.java" code.



please help...........
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a little tied up with something else (and shall be for most of the coming week) but this is precisely what I have been advising against:Without having tried it, I feel you can use the methods of the Utilities class so that you examine only the complete words around the offset.

I rather think you need to also pass the length of the inserted string, for an insertion. Then use getPreviousWord(...) for the offset, and getNextWord(...) for the offset+string.length() and examine and highlight only within that range. Why search through a whole lot of stuff that hasn't just changed?
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:I'm a little tied up with something else (and shall be for most of the coming week)


OK, Now I feel bit sad But I really appreciate you helped me even when you are very busy with your work. Thanks a lot for that..


I am on my way to check your suggestions. Unfortunately, we have heavy rains here and I have to wait until this rain is over to turn on my computer
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks a lot for the help!! It worked!!! Here is my code



Anyway, I had to use getWordEnd() instead of getNextWord() because getNextWord() is keep on throwing exceptions.

Thanks again for the help! I really appreciate it!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic