Help me with this question. I want to add a proress bar on button click and while progress bar is running the main window(frame) should be closed or hidden. as progress finishes the main window should appear.
Asked
Active
Viewed 2,124 times
-3
-
Check out the [progress bar tutorial](http://docs.oracle.com/javase/tutorial/uiswing/components/progress.html) from Oracle – Robin Nov 18 '12 at 09:25
2 Answers
2
there are two direct ways (implemented in APIs)
notice Swing GUi isn't thread safe, all output to Swing GUi must be done on Event Dispatch Thread (EDT)
0
You can find an example with a complete working code for your problem here:
http://www.roseindia.net/java/example/java/swing/SwingProgressBar.shtml
You only need to add a mainframe.setVisible(false);
and a mainframe.setVisible(true);
to it, where mainframe is your main JFrame. You can do this modification in the "Create a timer" part, with a new if (i==0)
and the existing if (i==20)
statement. Than delete or modify tha rest of the code for your needs.

totymedli
- 29,531
- 22
- 131
- 165
-
It's not a particularly useful example (IMHO). While the `Timer` ensures that the updates are performed within the EDT, the don't actually show how to effectively run a background to task so as not to block the EDT AND sync the progress update calls to the EDT – MadProgrammer Nov 18 '12 at 08:03