I have written a small program in Java, which performs complicated and
long-lasting calculations. So I want to illustrate the progress of
these calculations by a text (percent work done) placed in JLabel
component. But I have a "small" problem - this text doesn't change
during the calculations and after them it is set at once to 100%. The
source code is as follows :
percent = 0;
for (....)
{
// calculations
percent +=... //small progress value
Label2.setText(Double.toString(percent));
}
I have searched Web and read this can be caused by non-synchronized
threads of GUI and rest of program, but I don't know how it can be
repaired. I've already tried one method (shown below) but it also
doesn't work (effects are the same as in first program):
static void setTextOnLabel(final String text)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
jLabel2.setText(text);
}
});
}
....
percent = 0;
for (....)
{
// calculations
percent +=...
setTextOnLabel(Double.toString(percent));
}
Can anyone help me? Thanks in advance
Chrosciu
Roland - 27 Jan 2005 13:47 GMT
> I have written a small program in Java, which performs complicated and
> long-lasting calculations. So I want to illustrate the progress of
[quoted text clipped - 56 lines]
>
> Chrosciu
Read up on "How to Monitor Progress"
<http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html>
and "How to Use Threads"
<http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html>

Signature
Regards,
Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
David Segall - 27 Jan 2005 14:14 GMT
>I have written a small program in Java, which performs complicated and
>long-lasting calculations. So I want to illustrate the progress of
>these calculations by a text (percent work done) placed in JLabel
>component. But I have a "small" problem - this text doesn't change
>during the calculations and after them it is set at once to 100%.
<http://www.javaworld.com/javaworld/jw-06-2003/jw-0606-swingworker.html>
provides a concise description of the problem and a solution. I
downloaded SwingWorker from
<http://java.sun.com/products/jfc/tsc/articles/threads/threads2.html>
The latter site also answers your next question :) which is "How do I
let the user interrupt my long calculation?"
[snip]