JTextPane Text Coloring
I'm attempting to make a text editor - a programmer's tool - that colors certain keywords as they are typed in. I'm using a JTextPane and a DocumentListener to wait for keypresses that modify the document text. When each letter is typed, the listener expands the offset and the length to include the entire word. This works, I've checked. If I typehello the program recognizes it as a word. If I press the spacebar and then typetest the program recognizestest as a separate word, the current one.
However, when I try to removeString from the document, or try to insertString with a MutableAttributeSet that is not the LogicalStyle of the document, it gives me this error:
Exception occurred during event dispatching:
java.lang.IllegalStateException: Attempt to mutate in notification
at javax.swing.text.AbstractDocument.writeLock(AbstractDocument.java:1090)
at javax.swing.text.AbstractDocument.insertString(AbstractDocument.java:511)
at Language.Editor.ColorCommand.insertUpdate(ColorCommand.java:63)
at javax.swing.text.AbstractDocument.fireInsertUpdate(AbstractDocument.java:180)
at javax.swing.text.AbstractDocument.insertString(AbstractDocument.java:542)
at javax.swing.JTextPane.replaceSelection(JTextPane.java:159)
at javax.swing.text.DefaultEditorKit$DefaultKeyTypedAction.actionPerformed(DefaultEditorKit.java:801)
at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1384)
at javax.swing.JComponent.processKeyBinding(JComponent.java:2078)
at javax.swing.JComponent.processKeyBindings(JComponent.java:2104)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2050)
at javax.swing.JEditorPane.processKeyEvent(JEditorPane.java:1159)
at javax.swing.text.JTextComponent.replaceInputMethodText(JTextComponent.java:2793)
at javax.swing.text.JTextComponent.processInputMethodEvent(JTextComponent.java:2654)
at java.awt.Component.processEvent(Component.java:3558)
at java.awt.Container.processEvent(Container.java:1164)
at java.awt.Component.dispatchEventImpl(Component.java:2593)
at java.awt.Container.dispatchEventImpl(Container.java:1213)
at java.awt.Component.dispatchEvent(Component.java:2497)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:339)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:131)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:98)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:85)
Anyone have any idea how I can fix this?

