Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Part of your error is that you are accessing a Swing component from outside the event thread! Yes, <a href="http://java.sun.com/javase/6/docs/api/javax/swing/JEditorPane.html#setText(java.lang.String)" rel="noreferrer">setText()</a> is thread-safe, but Swing methods are not Thread-safe unless they are explicitly declared as such. Thus, <a href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#setCaretPosition(int)" rel="noreferrer">setCaretPosition()</a> is not Thread-safe and must be accessed from the event thread. This is almost certainly why your application eventually freezes.</p> <p>NOTE: <code>JTextPane</code> inherits its <code>setText()</code> method from <code>JEditorPane</code> and its <code>setCaretPosition</code> method from <code>JTextComponent</code>, which explains the links in the previous paragraph not going to the <a href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextPane.html" rel="noreferrer"><code>JTextPane</code></a> JavaDoc page.</p> <p>To be Thread-safe, you really need to at least call <code>setCaretPosition()</code> from within the event thread, which you can do with code like this:</p> <pre><code>SwingUtilities.invokeAndWait(new Runnable() { public void run() { a.setText(b.toString()); a.setCaretPosition(b.length()); } } </code></pre> <p>And since you have to call <code>setCaretPosition()</code> from within the event thread, you might as well also call <code>setText()</code> from the same place.</p> <p>It's possible that you may not need to manually set the caret position. Check out the section "Caret Changes" in the JavaDoc for <a href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html" rel="noreferrer">JTextComponent</a>.</p> <p>Finally, you may want to check out a series of two articles:</p> <ul> <li><a href="http://javatechniques.com/blog/faster-jtextpane-text-insertion-part-i/" rel="noreferrer">Faster JTextPane Text Insertion (Part I)</a></li> <li><a href="http://javatechniques.com/blog/faster-jtextpane-text-insertion-part-ii/" rel="noreferrer">Faster JTextPane Text Insertion (Part II)</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload