Problems with synchronization in JLabel (and Swing at all)

From: Chrosciu (chrosciu_at_poczta.onet.pl)
Date: 01/27/05


Date: 27 Jan 2005 04:11:14 -0800

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



Relevant Pages

  • Re: Waiting for Dialog to update in a Nnew Thread
    ... >I have a main thread that controls all of the UI and calculations. ... >during these calculations but the progress box is vizible. ... using strcpy? ...
    (microsoft.public.vc.mfc)
  • Re: problem with screen updates
    ... my program performs a series of calculations. ... >bar which updates as my calculations are progressing. ... >I have attempted to write a series of "progress commands" to the main window ... it perform the display operations. ...
    (microsoft.public.win32.programmer.ui)
  • Waiting for Dialog to update in a Nnew Thread
    ... I have a main thread that controls all of the UI and calculations. ... I want a list control in the progress box to say what stage the calculations ... void CProgressThread::Add ...
    (microsoft.public.vc.mfc)
  • Re: Accessing variables inside a function
    ... time limit. ... function's local variables to save them before this happens. ... calculations time out? ...
    (comp.soft-sys.matlab)
  • Re: Problems with synchronization in JLabel (and Swing at all)
    ... On 27-1-2005 13:11, Chrosciu wrote: ... > these calculations by a text placed in JLabel ... > doesn't work (effects are the same as in first program): ... Read up on "How to Monitor Progress" ...
    (comp.lang.java.gui)