Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2005

Tip: Looking for answers? Try searching our database.

Problems with synchronization in JLabel (and Swing at all)

Thread view: 
Chrosciu - 27 Jan 2005 12:11 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%. 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]


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.