How to get output on screen with java?

How to get output to screen while an action is running? (Java)

  • I have a Java program in which an action is initiated which loops through a list of items, does some calculations on the data, and various other tasks. The process takes about 10 minutes altogether, but I would like to output the results for each item in the list as the processing of that item is completed. The output is a set of items written to cells in a table. There is also a progress bar which does not update until the whole action completes. Despite output statements which are called during the loop through the list of items, all output seems to be queued up and only output after the entire action comes to an end. I've seen refresh procedures in other languages to get around this. Does Java allow such a refresh? Thanks in advance for any help with this.

  • Answer:

    Yes, Java does allow for the update of a GUI while long running processes are in progress. Take a look at concepts around http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html and the http://java.sun.com/javase/7/docs/api/javax/swing/SwingWorker.html class for some background. The output is a set of items written to cells in a table. There is also a progress bar which does not update until the whole action completes. The reason why you are not updating your widgets is that most likely you are doing your long running process in the lone Swing painting thread. This will stop your GUI from updating until the process is over. For a more accurate diagnosis, please post some code.

John R Doner at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

You could spin off http://java.sun.com/docs/books/tutorial/essential/concurrency/ to do the various processes.

davecoulter

Try System.out.flush(); after using System.out.println(); to write a message. This will flush the stream to screen.

Chris Dennett

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.