This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
UI Update in a Thread !
Posted by L.COPPEAUX on May 22, 2000 at 11:36 AM
> > Hi, > > I'm developing one application, in which when I start downloading > > the information, I want to show its status to user to indicate the > > amount of downloading finished. But I'm facing the problem. > > > > Here is what I had done :- > > Before I start downloading I starts the thread which is Frame > > and in that I'm displaying JLabel which should update status of > > downloading.I will update this JLabel in the loop as the one after > > one productsInformation is downloaded. > > After thread has been started, I will start downloading proudctsInfo > > in my main thread. But strange thing is that my spawned thread, > > doesn't update the status of downloading. In fact, it doesn't get > > repainted until all downloading is finished. Once it is finished, it > > displays the Frame that was spawned from the main Application. > > Can anybody please guide me in ths ? > > Any code sample will be most appericated . > > regards, > > raju > Have you tried wait() and notify() methods i.e. stopping/allowing the main thread to sleep for a while and repaint the frame in the mean time ? You can't update the UI inside a thread directly. All the UI work must be done in the event-dispatching thread. When, you want to update the UI inside a thread, you need to use : SwingUtilities.invokeLater(new Runnable(){ public void run(){ // UI update Work } }); invokeLater put the runnable in the event queue and therefore the UI is updated in a thread-safe manner. Regards
Replies:
|