Problem updating a label

J

James Kimble

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

Thomas Weidenfeller

James said:
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
 
N

NullBock

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.
 
J

James Kimble

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

Roedy Green

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

gui problem 1
Problem with Email Program in TCP 1
print to a label printer directly 1
GUI 14
Newbe, paintComponent problem. 1
Applet Hangs when submitting data to servlet 21
Animation Problem 15
Need help on Swing 2

Members online

No members online now.

Forum statistics

Threads
473,816
Messages
2,569,710
Members
45,498
Latest member
SharylPont

Latest Threads

Top