Problem updating a label

  • Follow


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!!

0
Reply jkimble (57) 1/13/2006 1:43:13 PM

James Kimble wrote:
> 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
-- 
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/
0
Reply nobody89 (1419) 1/13/2006 2:12:35 PM


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.

0
Reply funny_leech (13) 1/13/2006 2:15:52 PM

Thanks very much! I'm sure that'll fix it.

0
Reply jkimble (57) 1/13/2006 3:13:32 PM

I implemented your solution (yes I copied it!!!) and it works great.
Thanks again!

0
Reply jkimble (57) 1/13/2006 6:16:24 PM

On 13 Jan 2006 05:43:13 -0800, "James Kimble" <jkimble@one.net> wrote,
quoted or indirectly quoted someone who said :

>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
-- 
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
0
Reply my_email_is_posted_on_my_website (4730) 1/14/2006 5:05:42 AM

5 Replies
33 Views

(page loaded in 0.106 seconds)

Similiar Articles:













7/19/2012 10:03:47 PM


Reply: