JTextArea is not automatically scrolled on printing in SwingWorker
<p>My problem is that the <code>JTextArea</code> with <code>JScrollPane</code> added is not scrolling to the newest line printed (the scroll bar stays at the top).<br>
The caveat is that lines are printed in the separate <code>SwingWorker</code> thread.</p>

<p>Whereas, if I get rid of <code>SwingWorker</code> in the <code>simulation()</code>, the scrolling seems to be working fine.</p>

<p>Can someone please give me a hint how to cope with this?</p>

<p>Here is the code I am testing on (<code>import</code>s omitted):</p>

<pre><code>public class Test4 {

    private JFrame frame;
    private JTextArea textArea;
    private JButton btnNewButton;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Test4 window = new Test4();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Test4() throws InterruptedException {
        initialize();
    }

    private void initialize() throws InterruptedException {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        textArea = new JTextArea();
        textArea.setEditable(false);

        JScrollPane scroll = new JScrollPane(textArea);
        scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        frame.getContentPane().add(scroll, BorderLayout.CENTER);

        btnNewButton = new JButton("Test Scrolling");
        frame.getContentPane().add(btnNewButton, BorderLayout.NORTH);
        btnNewButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                simulation();
            }
        });

    }

    private void simulation() {
        SwingWorker&lt;Void, Void&gt; swingWorker = new SwingWorker&lt;Void, Void&gt;() {

            @Override
            protected Void doInBackground() throws Exception {
                TestOutputStream customOutputStream = new TestOutputStream(
                        textArea);
                System.setOut(new PrintStream(customOutputStream, true, "UTF-8"));
                for (int i = 0; i &lt; 30; i++) {
                    System.out.println("TEST");
                }
                return null;
            }
        };
        swingWorker.execute();
    }
}

class TestOutputStream extends OutputStream {
    private final ByteArrayOutputStream buf = new ByteArrayOutputStream();
    private final JTextArea consoleOutput;

    public TestOutputStream(JTextArea consoleOutput) {
        super();
        this.consoleOutput = consoleOutput;
    }

    @Override
    public void flush() throws IOException {
        consoleOutput.append(buf.toString("UTF-8"));
        buf.reset();
    }

    @Override
    public void write(int b) throws IOException {
        buf.write(b);
    }
}
</code></pre>

<p><a href="https://i.stack.imgur.com/HtmWu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HtmWu.png" alt="Screenshot"></a></p>