The contents of the JTextPane are as follows: "t. x"
The routine that marks up the text pane, after the text has been
inserted is as follows:
private StyledDocument document;
private void addMarkup(final SimpleAttributeSet attribute,
final int offset, final int length) {
System.out.println("START: " + offset + " LENGTH: " + length + " ATTRIBUTE: " + attribute);
try {javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
document.setCharacterAttributes( offset, length, attribute, false);
}});} catch (Exception e) {System.err.println(e);}
}
When run, the program generates the following output:
...
START: 0 LENGTH: 2 ATTRIBUTE: foreground=java.awt.Color[r=255,g=0,b=0]
START: 3 LENGTH: 2 ATTRIBUTE: foreground=java.awt.Color[r=0,g=0,b=0]
Exception occurred during event dispatching:
javax.swing.text.StateInvariantError: infinite loop in formatting
at javax.swing.text.FlowView$FlowStrategy.layout(FlowView.java:429)
at javax.swing.text.FlowView.layout(FlowView.java:182)
at javax.swing.text.BoxView.setSize(BoxView.java:265)
at javax.swing.text.BoxView.layout(BoxView.java:600)
at javax.swing.text.BoxView.setSize(BoxView.java:265)
at javax.swing.plaf.basic.BasicTextUI$RootView.setSize(BasicTextUI.java:1450)
at javax.swing.plaf.basic.BasicTextUI.getPreferredSize(BasicTextUI.java:688)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1042)
at javax.swing.JEditorPane.getPreferredSize(JEditorPane.java:1065)
at javax.swing.ScrollPaneLayout.layoutContainer(ScrollPaneLayout.java:752)
at java.awt.Container.layout(Container.java:686)
at java.awt.Container.doLayout(Container.java:676)
at java.awt.Container.validateTree(Container.java:750)
at java.awt.Container.validate(Container.java:728)
at javax.swing.RepaintManager.validateInvalidComponents(RepaintManager.java:313)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:204)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:154)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:317)
at java.awt.EventDispatchThread.pumpOneEvent(EventDispatchThread.java:103)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:84)
...
Several other StateInvariantError's are thrown, and the program
eventually freezes.
We can't figure out how to avoid or work around these
StateInvariantError's, and they don't appear in the java
documentation. What are they, and how to we avoid them?
--
Michael Duggan
(md...@cs.cmu.edu)
<code and stack trace snipped>
>
> Several other StateInvariantError's are thrown, and the program
> eventually freezes.
>
> We can't figure out how to avoid or work around these
> StateInvariantError's, and they don't appear in the java
> documentation. What are they, and how to we avoid them?
The setCharacterAttributes() method fires a DefaultDocumentEvent as
specified in the documentation. If you are calling addMarkup() as a
result of some DocumentEvent, the result will be an infinite loop.
Jim S.