I have a JFrame with progress bar and my code which should run it in background showing the progress in progress bar.
I implemented runnable in my Progressbar class and started the thread. But the progress bar frame is not displaying full.. and it gets stuck and it display full after my code is fully executed i.e after main thread close.
I know this is some basic mistake.
public class ProgressScriptUI extends JFrame implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
setTitle("Progressing to Generate DDL Scripts");
setBounds(400, 250, 850, 400);
getContentPane().setLayout(null);
JProgressBar progressBar= new JProgressBar(0,100);
progressBar.setBounds(200, 100, 500, 20);
add(progressBar);
setVisible(true);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
//I am calling the below code in some other class
ProgressScriptUI progress = new ProgressScriptUI();
Thread uiThread = new Thread(progress);
uiThread.start();
oracleValidateOLDorNEW.execute(); //Code that I need to call in back ground
解决方案
All UI code must be executed within the context of the Event Dispatching Thread. This makes it a little tricky to do work in the background while keeping the UI up to date.
Take a look at Concurrency in Swing for suggestions and recommendations for solutions.
Probably the easiest would be to use a SwingWorker which has functionality to publish/process data so that it is synced back to the EDT as well as progress callbacks
You can take a look at
As some examples....