aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Issue in Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Issue in "Styling" JTextPane" Watch "Issue in "Styling" JTextPane" New topic
Author

Issue in "Styling" JTextPane

Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 499

Hi,

Please have a look at the following code




In this code, I am trying to change the colour of the text in the given position. But, it ends up with the following exception.




How could I make this correct? Please help...


Are you better than me? Then please show me my mistakes..
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

You can't update the document from the events that are triggered when it's updated. Otherwise you could trigger an infinite loop - updating would cause updating would cause updating. While that makes sense for textual updates, for attributes it may be a bit harsh.
The solution is surprisingly simple: make sure the updating is done at a later time, using EventQueue.invokeLater*:

* SwingUtilities.invokeLater calls EventQueue.invokeLater.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 499

Thanks a lot for the reply Rob. It works!

But however, it didnt work for the following, my original code



In there, it simply freeze after colouring two or three words!! Please help!!>
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

After slightly modifying that code to get it to compile, I've added some debugging statements, and as soon as the first keyword was entered it entered an infinite loop. I've checked the source of DefaultStyledDocument and it fires a changedUpdate event when you call setCharacterAttributes. That means that you need to disable updates while you call this method.

I can think of two ways to do this:
1) keep a reference to your DocumentListener, remove it from the document before calling the method, then add it again afterwards.
2) keep a boolean, initially set to false, that you set to true before this call and to false after this call. Inside the event listener you check this boolean value and if it's true you don't do anything.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

3) Use a DocumentFilter that conditionally calls into the super implementation or the FilterBypass's implementation.

Note: This code is woefully inefficient as it sets the attributes throughout the document at every change. I haven't tested whenther this will cause a performance hit with a longer keyword list and a long document.

edit Rectified an omission in the code


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

Joined: Oct 07, 2010
Posts: 499

Thanks a lot to both of you!!! It is amazing!!! I tested the suggestions with my 100% original code with more than one styles!! It worked!!! Thanks a lot again..I really appreciate it!!!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

You're welcome.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Issue in "Styling" JTextPane