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 / General / January 2006

Tip: Looking for answers? Try searching our database.

Problem updating a label

Thread view: 
James Kimble - 13 Jan 2006 13:43 GMT
I'm using a JLabel to indicate the status of a program I'm running in
another thread. The problem is that I want to do a "label.setText" to
"Please wait", let the other thread run (the call waits until it
returns), then use "label.setText" to display the result. The problem
is the "Please wait" is never seen. The result is displayed but no
matter what I do the label stays in it's initial state, telling the
user to "Press the run button", until the called program completes and
then the results are displayed. No "Please wait" in between. It's
making me nuts!! What the heck is going on!

The code is below:

  There are a set of selection buttons that initially set label l3 as
in:

       l3.setText ("Press Run Check to continue");

  Then the run button trys to set it to Please Wait util the results
are displayed:

       JButton b4 = new JButton ("Run Check");
       b4.setBounds(60, 225, BTN_WD, BTN_HT);
       b4.addActionListener( new ActionListener()
       {
           public void actionPerformed(ActionEvent e)
           {
               strResult[0] = "10";

               l3.setText ( "Please wait....." );

               strResult = Utils.execCmdAndWait(command,true);

               log.info ( "Integrit returned result: " + strResult[0]
);

               if ( result.equals("1") )
               {
                   l3.setText ( "RESULT: Possible file corruption!" );
               }
               else
               {
                   l3.setText ( "RESULT: Files verified good!" );
               }

           }
       });

Only the initial state and RESULT is displayed. The call to CmdAndWait
can take several minutes. During that time the label stays in it's
initial state until the call returns when the label changes to the
RESULT: text.

Any help here greatly appreciated. I think it's a swing update thing
but calling repaint() doesn't help either. AHHHHHHHHHHHHHHHHHHH!!
Thomas Weidenfeller - 13 Jan 2006 14:12 GMT
> Only the initial state and RESULT is displayed. The call to CmdAndWait
> can take several minutes. During that time the label stays in it's
> initial state until the call returns when the label changes to the
> RESULT: text.

Sound's like a classic: You are blocking the event dispatching thread.
See the Top 5 list in the comp.lang.java.gui FAQ.

> Any help here greatly appreciated. I think it's a swing update thing
> but calling repaint() doesn't help either. AHHHHHHHHHHHHHHHHHHH!!

I think it's non compliance to the rules set by the Swing architecture.

/Thomas
Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

NullBock - 13 Jan 2006 14:15 GMT
This is a simple threading problem.  You're doing all your work in the
event thread, so your JLabel (along with the rest of your GUI) doesn't
get updated till after you're done with the operation.

You need to do something like this:

class MyClass {
 ActionListener l = new ActionListener() {
   public void actionPerformed(ActionEvent e) {
     l3.setText("Please wait.....");
     new Thread("worker thread") {
       public void run() {
         doIt();
       }
     }.start();
   }
 };

 private void doIt() {
   strResult[0] = "10";
   strResult = Utils.execCmdAndWait(command,true);
   log.info ( "Integrit returned result: " + strResult[0]);

   final String msg;
   if ( result.equals("1") ) {
     msg = "RESULT: Possible file corruption!";
   } else {
     msg = "RESULT: Files verified good!";
   }
   SwingUtilities.invokeLater(new Runnable() {
     public void run() {
       l3.setText(msg);
     }
   });
 }
}

Hope this helps,

Walter Gildersleeve
Freiburg, Germany

______________________________________________________
http://linkfrog.net
URL Shortening
Free and easy, small and green.
James Kimble - 13 Jan 2006 15:13 GMT
Thanks very much! I'm sure that'll fix it.
James Kimble - 13 Jan 2006 18:16 GMT
I implemented your solution (yes I copied it!!!) and it works great.
Thanks again!
Roedy Green - 14 Jan 2006 05:05 GMT
>Please wait" is never seen. The result is displayed but no
>matter what I do the label stays in it's initial state, telling the
>user to "Press the run button", until the called program completes and
>then the results are displayed. No "Please wait" in between. It's
>making me nuts!! What the heck is going on!

You are tying up the Swing thread. You never let it have a chance to
do the painting.  See http://mindprod.com/jgloss/swingthreads.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



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



©2009 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.