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
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:
- How to get location of NSStatusItem on the screen?Best solution by Stack Overflow
- How to get actual return type of a generic static method in Java?Best solution by stackoverflow.com
- How to get real path of an image in Java?Best solution by Stack Overflow
- How to Get Main output into Common() method in Java?Best solution by pages.cs.wisc.edu
- How do I get a new screen name for yahoo messenger?Best solution by sg.answers.yahoo.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.