Solved

Updating a Status Label imediately

Posted on 2002-03-12
10
224 Views
Last Modified: 2010-03-31
I have an app which has a status label which i update when doing fetching and updating!! the problem is that my label doesnt get updated until after all the work is done!! this is because of the way the awt dispathcher thread handles events(i.e it doesnt let go of one until it is finished and then moves on to the next one)!

so my code would look like this

myLabel.setText("Retrieving Data")
.
.
Retrieve Data + do some processing
.
.
myLabel.setText("Data Retrived");

now all the user sees is the Data Retrieved message, the Retrieving Data one does not get shown!! now i have a sneaky suspicion that what happens is that setText generates another awt dispatch event and this gets put on the queue behind the current event and doesnt get updated until the event has finished and thus the last one gets shown. i know i can run off my processing in another thread and that way my label will get updated but this means alot of changes around the code to show my label updating!! is there anyway i can get my setText to update straight away without having to do this???
0
Comment
Question by:j_baxter99
  • 2
  • 2
  • 2
  • +3
10 Comments
 
LVL 5

Expert Comment

by:nebeker
ID: 6856893
You need to do your processing and data retrieval in a background thread...
0
 
LVL 5

Expert Comment

by:nebeker
ID: 6856898
Also, you should never do any type of lengthly processing in an event handler.  It causes problems like the one you're experiencing...
0
 
LVL 9

Expert Comment

by:Venci75
ID: 6857108
try this:
      myLabel.setText("text");
      myLabel.repaint();
force repaint of the label after the text has been changed
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 7

Accepted Solution

by:
Igor Bazarny earned 100 total points
ID: 6857196
Venci75,

Your solution at least not reliable. repaint() posts request to repaint sometimes later. If awt event queue is blocked, repaint won't be handled.

nebeker is right, it's a bad idea to put long processing into event handler just beecause of reason j_baxter99 noted--following event won't be processed. Put processing into separate thread or split it into smaller steps and use SwingUtilities.invokeLater(nextStep) to continue processing. In this case all processing will be done in awt thread, but will need additional work for splitting.

In separate thread scenario, note that swing is not thread safe, so best way to set label text is:
SwingUtilities.invokeLater( new Runnable(){
    public void run(){
        myLabel.setText("Processing in progress");
    }
});

Regards,
Igor Bazarny,
Brainbench MVP for Java 1
www.brainbench.com


0
 
LVL 3

Expert Comment

by:randyd
ID: 6862033
if the thread suggestion is ignored, then the paintImmediately() method should force the update anyway...
randy
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6863753
To randyd.

Other problems will occur. Window will not repaint itself till process completion no matter what happens outside of it. For example when window is covered by another window and later uncovered.

Igor
0
 

Author Comment

by:j_baxter99
ID: 6864504
so in other words i cant avoid the setLabel label generating a repaint event which waits in the awt dispatch event queue waiting for my process to finish!!!
0
 

Author Comment

by:j_baxter99
ID: 6864508
ah by the way the SWingWorker way will work but as i said before it means a lot of changes for me!!
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7901850
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Answered by: bazarny
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venci75
EE Cleanup Volunteer
0
 
LVL 5

Expert Comment

by:Netminder
ID: 7962640
Per recommendation, force-accepted.

Netminder
EE Admin
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java SE 8u111  Lot of stuff broke 11 69
egit plugin on eclipse 8 63
javap bin 2 34
Way to decrease size of apk file 9 67
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question