I am trying to execute a program which takes around 6-7 seconds to execute after displaying GUI for the user. Problem here is that the user has to wait for 6-7 seconds without any processing or progress notification.
So want i am trying to do is that, while the MainApp ma = new MainApp(); code executes, i want to display a gif picture for the user saying that the program is being executed. Displaying of picture code is written in ShowProgressPic spp = new ShowProgressPic();
I have a boolean datatype variable named end which will be set to true in the last statement of MainApp() constructor. When the execution of the MainApp ma = new Mai开发者_开发技巧nApp(); ends, i want to stop displaying the gif image.
Please help me out with this. Thank you.
Assuming that you're problem involves a Swing GUI, I would suggest that you use a SwingWorker to do the long-acting program execution in a background thread. Then when the thread is done, you have two or more ways of proceeding from there. You can have the SwingWorker call code in its done method to close the progress display or you can have the code that holds and executes the SwingWorker listen to the SwingWorker via a PropertyChangeListener and act when the state property is SwingWorker.StateValue.DONE.
look into threads. You would have to load the process in a different thread from the main therad to show the progress. For showing the progress you could use JProgressBar
here is a part-pseudocode example example with a output print for displaying percent:
int maxSufftoDo = 500;
new Thread() {
public void run() {
while not done {
do part of stuff;
updateStatus(numOfStuffDone * 100 / maxStufftoDo);
}
}
}.start();
public void updateStatus(int s) {
System.out.println(i + "% done");
}
You should use Events and Listeners for multithreading in GUI (and in general also recommended).
精彩评论