0

I looked at Scroll JScrollPane to bottom and found the answer =>

JScrollBar vertical = scrollPane.getVerticalScrollBar();
vertical.setValue( vertical.getMaximum() );

to work very well.

However it doesn't go right to the bottom when there's a lot of output.

I've now added a delay (not from the EWT) and then another update in the EWT, which does the trick.

    Thread thread = Thread.currentThread();
        try {
            thread.sleep(99);
        } catch (InterruptedException ex) {
    }
    SwingUtilities.invokeLater(new Runnable() {
        public void run() { log(""); }
    });
Community
  • 1
  • 1
bob
  • 35
  • 6

1 Answers1

0

Is there an easy way to wait for all the output calls to finish?

Don't know exactly what you are doing, but typically you don't want to use Thread.sleep(...).

Instead you can wrap your code in a SwingUtilities.invokeLater(...). This will place the code at the end of the Event Dispatch Thread (EDT) so it can execute after other processing is finished.

SwingUtilities.invokeLater(new Runnable()
{
    public void run()
    {
        JScrollBar vertical = scrollPane.getVerticalScrollBar();
        vertical.setValue( vertical.getMaximum() );
    }
});

Read the section from the Swing tutorial on Concurrency in Swing for more information on the EDT and why this is important.

Maybe a better solution for your code is to use a SwingWorker as described in the tutorial.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Does this still go on the EDT if I'm in a swingWorker thread? – bob Mar 25 '17 at 19:42
  • The point of using the SwingWorker is that it gives you and API to execute some code in a separate Thread, but then execute other code on the EDT Thread. When you "publish" a result that automatically executes on the EDT. When you implement the done() method that also executes in the EDT. Read the tutorial again for more information about the SwingWorker including the working examples. – camickr Mar 25 '17 at 20:11