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开发者_JAVA技巧 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.
Yes, Java does allow for the update of a GUI while long running processes are in progress. Take a look at concepts around concurrency in Swing and the SwingWorker 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.
You could spin off worker threads to do the various processes.
Try System.out.flush();
after using System.out.println();
to write a message. This will flush the stream to screen.
精彩评论