File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Highlighting Text in JTextPane is SUPER SLOW!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Highlighting Text in JTextPane is SUPER SLOW!!" Watch "Highlighting Text in JTextPane is SUPER SLOW!!" New topic
Author

Highlighting Text in JTextPane is SUPER SLOW!!

Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 497

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

Are you better than me? Then please show me my mistakes..
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 497

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

Joined: Oct 07, 2010
Posts: 497

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

Joined: May 03, 2008
Posts: 4523
    
    5

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

Joined: Oct 07, 2010
Posts: 497

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

Joined: May 03, 2008
Posts: 4523
    
    5

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

Joined: Oct 07, 2010
Posts: 497

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

Joined: Oct 07, 2010
Posts: 497

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!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Highlighting Text in JTextPane is SUPER SLOW!!
 
Similar Threads
StyleAttributes not applying when called in quick succession
How to show arabic and arabic in a JTextBox when user enter the numbers?
Is It Possible To Set Input Numbers for JTextFiled?
Issue in "Styling" JTextPane
How to track backspace key?