I have a save button in a JFrame
;on clicking save the 'save' text sets to 'saving....'; I need to set that text as 'saved' after a delay of 10 seconds.How is it possible in java?
Please help...
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
This is what i did...but this wont shows as 'saving' during that delayed time.
This question & first 3 answers are heading down the wrong track.
JProgressBar
to show something is happening. Set it to indeterminate if the length of the task is not known, but presumably you know how much needs to be saved and how much is currently saved.SwingWorker
for long running tasks. See Concurrency in Swing for more details.The best is to use a timer and its method execute with a delay : http://developer.apple.com/library/mac/documentation/java/reference/javase6_api/api/java/util/Timer.html#schedule(java.util.TimerTask, long). Use a timertask to wrap your runnable and that's it.
If you want to provide the user with visual feedback that something is going on (and maybe give some hint about the progress) then go for
JProgressBar
andSwingWorker
(more details).If on the other hand you want to have a situation, when user clicks the button and the task is supposed to run in the background (while the user does other things), then I would use the following approach:
Finally, the initial solution that was using the
Swing
specific timer: