You have a classic problem with concurrency and Swing. Your problem is that you are performing a long-term task in the main Swing, EDT or Thread Dispatch Thread, and this will block the thread until the process is complete, preventing it from performing its tasks, including user interaction and drawing a graphical user interface.
The solution is to perform a long-term task in a background thread, such as that specified by a SwingWorker object. You can then update the progress indicator (if qualifier) ββusing the SwingWorker publish / process pair. Read more about this in the Concurrency article in Swing .
eg.
public void myMethod() { final MyProgessBarFrame progFrame = new MyProgessBarFrame(); new SwingWorker<Void, Void>() { protected Void doInBackground() throws Exception { // do some processing here while the progress bar is running // ..... return null; }; // this is called when the SwingWorker doInBackground finishes protected void done() { progFrame.setVisible(false); // hide my progress bar JFrame }; }.execute(); progFrame.setVisible(true); }
Also, if this is being displayed from another Swing component, you should probably show a modal JDialog, not a JFrame. This is why I called setVisible (true) in the window after the SwingWorker code, so if this is a modal dialog, it will not interfere with SwingWorker execution.
source share