jProgressbar e Jtextfield not updating

Hi! i am creating a little application: a JFrame. It reads from N xml files tags and write them in a DB. I'd like to add a JProgressBar and a jtextfield showing the progress status, so I added them and inside a "for" cycle I update them:

fileCorrente=0;

fileTotali = N;

// nrFiles = new JTextField()

//jProgressBar1 = new JProgressBar()

jProgressBar1.setMinimum(fileCorrente);

jProgressBar1.setMaximum(fileTotali);

jProgressBar1.setValue(fileCorrente);

jProgressBar1.setStringPainted(true);

for (int i=0; i < fileTotali;i++){

nrFiles.setText(fileCorrente+" / "+fileTotali);

importaFile(lstFiles[i]);//THE TASK

fileCorrente++;

jProgressBar1.setValue(fileCorrente);

nrFiles.setText(fileCorrente+" / "+fileTotali);

}

jProgressBar1.setValue(fileCorrente);

nrFiles.setText(fileCorrente+" / "+fileTotali);

It doesn't work! it only show 0/5 files --> 5/5 files

and the progressBar 0% --> 100&

why don't they get updated during the execution of "FOR"?

thanks in advance, Giacomo

[1439 byte] By [tuffo19suna] at [2007-11-27 4:41:14]
# 1

You're doing your time consuming task on the event dispatching thread, which is the same thread that updates the gui. Your gui doesn't get updated until the task is finished, so you don't see any intermediate results. Create a worker thread to do the heavy lifting, so the edt stays responsive.

hunter9000a at 2007-7-12 9:52:33 > top of Java-index,Desktop,Core GUI APIs...
# 2

Probably because all you code is executing in the GUI Event Thread so it doesn't get a chance to repaint itself until the task is finished. You need to create a separate Thread for the long running task. Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html]How to Use Progress Bars[/url] for a working example.

camickra at 2007-7-12 9:52:33 > top of Java-index,Desktop,Core GUI APIs...