The moose likes Swing / AWT / SWT and the fly likes IllegalStateException in a StyledDocument Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "IllegalStateException in a StyledDocument" Watch "IllegalStateException in a StyledDocument" New topic

IllegalStateException in a StyledDocument

Rey Fernandez

Joined: Aug 06, 2009
Posts: 10
is there any alternative for this code to run without exception?

The above code will launch a JFrame with a JTextPane on it, but it generates an IllegalStateException upon inserting a string to the JTextPane. My goal is to make the program react to the change in the JTextPane's document and this reaction to the change is by inserting a String to the same document where the event came from.Is there any other way to accomplish this task?
Rey Fernandez

Joined: Aug 06, 2009
Posts: 10
if the cause of is exception is the possible infinite invocation of the DocumentListener, is there a way where only text changes can be listened not font color, size or any other changes only the textcontent of the document will be listened?
Alan Mehio
Ranch Hand

Joined: Apr 04, 2005
Posts: 73
Rey Fernandez wrote:is there any alternative for this code to run without exception?

Document listeners shouldn't modify the contents of the document; The change is already complete by the time the listener is notified of the change

Your situation violates the bean event model where order of delivery is not guaranteed and all listeners should be notified before further mutations are allowed.

In your case, you are trying to mutate the document and still there are some listeners has not been notified. Simply, you try to triggers update inside an update.

The document has been update and you get the update; so simple display the updat in different place.

Please take a look at

Alan Mehio
London, UK
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2446
Use a DocumentFilter. An example can be found in the section from the Swing tutorial on "Text Component Features".
I agree. Here's the link:
subject: IllegalStateException in a StyledDocument
It's not a secret anymore!