Click to See Complete Forum and Search --> : Problem with Swing -.-


KhaosPrinz
December 7th, 2009, 01:11 PM
Hey guys,

i have a simple question.
i have a JLabel jlbl_status in which i want so show the status of the current process.

if i set the text with the setText() method the text is not displayed in that moment.
It will be displayed after the whole action...

for example:
i have a JButton which trigges the following

log.info("recalculate Values");
RecalculateTableValuesJob job;
try {
job = new RecalculateTableValuesJob();
}catch(Exception e){
log.error("Cant start the recalc. job");
e.printStackTrace();
return;
}
setStatus("Calculation...");
setProgress(0);
job.start();

while(job.isRunning){
try{Thread.sleep(1000); }catch(Exception e){}
int p = (int)(100*xlsData.getRowCount()/job.currentIndex);
setProgress(p);
}
setProgress(100);

log.info("recalculate Values Done");


btw. i also have the same problem with my JProgressBar ;-)

my setStatus looks like

public void setStatus(String text) {
log.debug("Status: "+text);
jlbl_status.setText(text);
}


i have tried also with
jlbl_status.invalidate();
jlbl_status.revalidate();
jlbl_status.repaint();

so PLEASE!! help me... why doesnt my JLabel repaint synchronously?

KhaosPrinz
December 8th, 2009, 02:40 AM
hey i found out, that when i do this action in my init of the program everything works fine.
So it has something to do with my buttonevent?!
Cuz only then it doent work -.-...

dlorde
December 8th, 2009, 05:24 AM
You need to update your label text in the Swing Event dispatch Thread (EDT), not directly from your calculation thread. Swing provides a SwingWorker class you should use for background work that makes updating Swing components on the EDT very simple.

See Concurrency In Swing (http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html) for a full explanation & tutorial, with examples.

It is a bad plan that admits of no modification...
P. Syrus

KhaosPrinz
December 8th, 2009, 12:28 PM
It works!! =)
You are a god ^^ why havn't i heard of this before?!...
Well, thanks dlorde