Posted on 2005-03-24
Medium Priority
Last Modified: 2012-06-27
HI Experts
I am developing and IDE for JAVA.I have made a DocumentListener which listens for any event and calls the Syntax Highlighter.When the file loads it is working absolutely fine,but when the document is modified it is giving IllegalStateException.How can i avoid this?
Thanx in advance
Question by:sonalibhalla
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
LVL 15

Expert Comment

ID: 13627791
I don't think this is enough information to help you out.
Do you have a stack trace? what operation throws the exception?
More data will be helpful

Author Comment

ID: 13627828

mat_syntax is one of the Matcher i have created.This is the part of code whichh is throws

{                                                 doc.setCharacterAttributes(mat.start()+1,mat.end()-mat.start()-2, style_blue, true);

Exception is
java.lang.IllegalStateException: Attempt to mutate in notification
                at javax.swing.text.AbstractDocument.writeLock(AbstractDocument.java:1335)
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI


Author Comment

ID: 13627888
here is the stack trace

java.lang.IllegalStateException: Attempt to mutate in notification
        at javax.swing.text.AbstractDocument.writeLock(AbstractDocument.java:133
        at javax.swing.text.DefaultStyledDocument.setCharacterAttributes(Default
        at parser.<init>(parser.java:45)
        at MyDocumentListener.insertUpdate(MyDocumentListener.java:37)
        at javax.swing.text.AbstractDocument.fireInsertUpdate(AbstractDocument.j
        at javax.swing.text.AbstractDocument.handleInsertString(AbstractDocument
        at javax.swing.text.AbstractDocument.insertString(AbstractDocument.java:
        at javax.swing.JTextPane.replaceSelection(JTextPane.java:189)
        at javax.swing.JTextPane.replaceSelection(JTextPane.java:153)
        at javax.swing.text.DefaultEditorKit$DefaultKeyTypedAction.actionPerform
        at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1502)
        at javax.swing.JComponent.processKeyBinding(JComponent.java:2422)
        at javax.swing.JComponent.processKeyBindings(JComponent.java:2457)
        at javax.swing.JComponent.processKeyEvent(JComponent.java:2385)
        at java.awt.Component.processEvent(Component.java:4830)
        at java.awt.Container.processEvent(Container.java:1380)
        at java.awt.Component.dispatchEventImpl(Component.java:3526)
        at java.awt.Container.dispatchEventImpl(Container.java:1437)
        at java.awt.Component.dispatchEvent(Component.java:3367)
        at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.ja
        at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboard
        at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeybo
        at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeybo
        at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFoc
        at java.awt.Component.dispatchEventImpl(Component.java:3396)
        at java.awt.Container.dispatchEventImpl(Container.java:1437)
        at java.awt.Window.dispatchEventImpl(Window.java:1566)
        at java.awt.Component.dispatchEvent(Component.java:3367)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:445)
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)

        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:130)

        at java.awt.EventDispatchThread.run(EventDispatchThread.java:98)
LVL 15

Expert Comment

ID: 13627900
Did you try to do as described in the link above?
Basically the solution there was something similar to:

remove yourself from being a document listener
{                                         doc.setCharacterAttributes(mat.start()+1,mat.end()-mat.start()-2, style_blue, true);

add yourself back as a document listener
LVL 15

Expert Comment

ID: 13627924
Also you can look at:
http://dbserv.jinr.ru/js/content/java/New-tutorial/ui/swing/text.html (search for "However, you should never modify the contents of text component from within a document listener"). They also provides an alternative solution

Author Comment

ID: 13635241
I tried removing the document listener but then the code below it doen not executes,because its being called by the document listener itself.Please sugges some other solution,

Author Comment

ID: 13635242
I could not find appropriate alternative in the article you mentioned.
LVL 15

Accepted Solution

aozarov earned 800 total points
ID: 13636074
The article explains that if you want to change your Document then you should not doing it from your DocumentListener but rather
you should extended the Document and overrides insertString.
Here is extract:
The Years field is an instance of WholeNumberField.java, which is a subclass of JTextField. By overriding the createDefaultModel method, WholeNumberField establishes a custom Document subclass -- an instance of WholeNumberDocument -- as the document for each WholeNumberField created:
protected Document createDefaultModel() {
    return new WholeNumberDocument();

Here's the implementation of WholeNumberDocument:
protected class WholeNumberDocument extends PlainDocument {

    public void insertString(int offs, String str, AttributeSet a)
                    throws BadLocationException {

        char[] source = str.toCharArray();
        char[] result = new char[source.length];
        int j = 0;

        for (int i = 0; i < result.length; i++) {
          if (Character.isDigit(source[i]))
              result[j++] = source[i];
          else {
              System.err.println("insertString: " + source[i]);
        super.insertString(offs, new String(result, 0, j), a);

This class overrides the insertString method which is called every time any string or character is about to be inserted into the document. WholeNumberDocument's implementation of insertString evaluates each character to be inserted into the text field. If the character is a digit, the document allows it to be inserted. Otherwise, the method beeps and prints an error message. Thus WholeNumberDocument allows the numbers in the range 0, 1, 2, ...

BTW, the remvoeListner and addListener (first suggestion) should be done in the DocumentListener prior and after the modification, but the previous
solution is more recommended.

Author Comment

ID: 13636773
Thanx a lot for all the help.
My case is a little different.I am not inserting any string in the document.I am just changing color of text.I think i will have to override some other function setCharacterAttributes may be.
LVL 15

Expert Comment

ID: 13636859
That is fine as long as you don't manipulate the Document from the listener itself.
Overriding any method in your Document to handle the a change should be appropriate.

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month11 days, 8 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question