Problem with using a timer to update progressbar
I have implement a timer to run a progress bar as follows
public class UpdateOnline extends JInternalFrame {
//UI related code goes here
class jbutton_proceed_action implements ActionListener {
public void actionPerformed(ActionEvent e) {
int result = -1;
Timer time = new Timer(50, updateBar);
time.start();
result = doUpload("bulk");
}
}
ActionListener updateBar = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int val = jProgressBar1.getValue();
jProgressBar1.setValue(++val);
}
};
}
what is happening is when I clicked on a button called upload, it performed action in class jbutton_proceed_action. The method called doUpload
upload data to a database which take mere than 2 minutes time to finished. My problem was eventhough I have start the timer before calling to doUpload method progr开发者_开发问答essbar doesnot start running untill doUpload method finished. Can some one pls help me to solve that problem.
I need to run the prgressbar mean while the uplod occurs
It is because doUpload
is running on the Swing (UI) EDT (event dispatch thread) and blocking it. Using SwingWorker is one way to handle the long-running task and still play nice. Make sure that all UI updates/manipulation only happens from the EDT -- the previous link (to the SwingWorker trail) contains the details.
You doUpload is running on the UI thread, causing it to block.
execute the doUpload in a new thread
http://download.oracle.com/javase/tutorial/uiswing/concurrency/worker.html
all you need is here:
http://en.wikipedia.org/wiki/SwingWorker
精彩评论