• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Issue in "Styling" JTextPane

 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic