?
Solved

IllegalStateException

Posted on 2005-03-24
13
Medium Priority
?
231 Views
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
0
Comment
Question by:sonalibhalla
[X]
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
13 Comments
 
LVL 15

Expert Comment

by:aozarov
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
0
 

Author Comment

by:sonalibhalla
ID: 13627828

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

while(mat_syntax.find()
{                                                 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)
0
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

by:sonalibhalla
ID: 13627888
here is the stack trace

java.lang.IllegalStateException: Attempt to mutate in notification
        at javax.swing.text.AbstractDocument.writeLock(AbstractDocument.java:133
5)
        at javax.swing.text.DefaultStyledDocument.setCharacterAttributes(Default
StyledDocument.java:338)
        at parser.<init>(parser.java:45)
        at MyDocumentListener.insertUpdate(MyDocumentListener.java:37)
        at javax.swing.text.AbstractDocument.fireInsertUpdate(AbstractDocument.j
ava:184)
        at javax.swing.text.AbstractDocument.handleInsertString(AbstractDocument
.java:748)
        at javax.swing.text.AbstractDocument.insertString(AbstractDocument.java:
705)
        at javax.swing.JTextPane.replaceSelection(JTextPane.java:189)
        at javax.swing.JTextPane.replaceSelection(JTextPane.java:153)
        at javax.swing.text.DefaultEditorKit$DefaultKeyTypedAction.actionPerform
ed(DefaultEditorKit.java:839)
        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
va:1700)
        at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboard
FocusManager.java:568)
        at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeybo
ardFocusManager.java:739)
        at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeybo
ardFocusManager.java:673)
        at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFoc
usManager.java:534)
        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
read.java:190)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:144)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)

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

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

Expert Comment

by:aozarov
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
while(mat_syntax.find()
{                                         doc.setCharacterAttributes(mat.start()+1,mat.end()-mat.start()-2, style_blue, true);
}

add yourself back as a document listener
0
 
LVL 15

Expert Comment

by:aozarov
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
0
 

Author Comment

by:sonalibhalla
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,
0
 

Author Comment

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

Accepted Solution

by:
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 {
              toolkit.beep();
              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.
0
 

Author Comment

by:sonalibhalla
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.
0
 
LVL 15

Expert Comment

by:aozarov
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.
0

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