Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It is likely a Swing concurrency issue in that you're adding items to the JList's model off of the event thread. This can tie up the thread and prevent it from doing its necessary actions of drawing the GUI or allowing for user interaction effectively freezing your Swing GUI.</p> <p>Try making sure that you only write to the JList on the EDT (the Swing Event Dispatch Thread). Something like this will queue the <code>addElement(...)</code> call on the Swing event thread:</p> <pre><code>public void write(final int b) { try { sentence = sentence + (char) b; if (sentence.endsWith("\n") &amp;&amp; !sentence.isEmpty() &amp;&amp; !sentence.equals("\t") &amp;&amp; !sentence.equals("\r\n") &amp;&amp; !sentence.equals("\n")) { if (writing == false) { writing = true; final String text = sentence.replaceAll("\n", "") .replaceAll("\r", "").trim(); javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { subject.addElement(text); } }); } sentence = ""; writing = false; } } catch (Exception e) { e.printStackTrace(Boot.stdErr); } } </code></pre> <p><strong>Edit 1</strong><br> This is closer to something I've done in the past, using a StringBuilder:</p> <pre><code>import java.io.IOException; import java.io.OutputStream; import javax.swing.DefaultListModel; import javax.swing.SwingUtilities; public class JListOutputStream extends OutputStream { private DefaultListModel logListModel; private StringBuilder sb = new StringBuilder(); public JListOutputStream(DefaultListModel logListModel) { this.logListModel = logListModel; } @Override public void write(int b) throws IOException { if (b == '\r') return; if (b == '\n') { final String text = sb.toString().trim(); sb.setLength(0); if (text.isEmpty()) { return; } SwingUtilities.invokeLater(new Runnable() { public void run() { logListModel.addElement(text); } }); return; } sb.append((char) b); } } </code></pre> <p><strong>Edit 2</strong><br> As per this article titled <a href="http://java.sun.com/developer/technicalArticles/Streams/WritingIOSC/index.html" rel="nofollow">Writing Your Own Java I/O Stream Classes</a>, if you're extending OutputStream, don't forget to provide custom implementations for the <code>flush()</code> and <code>close()</code> methods.</p>
 

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