• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

IllegalStateException

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
sonalibhalla
Asked:
sonalibhalla
  • 6
  • 5
1 Solution
 
aozarovCommented:
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
 
sonalibhallaAuthor Commented:

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
sonalibhallaAuthor Commented:
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
 
aozarovCommented:
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
 
aozarovCommented:
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
 
sonalibhallaAuthor Commented:
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
 
sonalibhallaAuthor Commented:
I could not find appropriate alternative in the article you mentioned.
0
 
aozarovCommented:
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
 
sonalibhallaAuthor Commented:
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
 
aozarovCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now