🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #8947

Re: ProgressBar doesn't update until FOR loop ends?

#8947
From: Steve Horsley
Date: Wed, 14 Jun 2000 00:00
37 lines
2149 bytes
A swing app has one event handling thread that loops looking for
event messages, and calls the appropriate event handler (listener)
when an event message arrives.

When you click the button, the event handler thread calls your click-
handler and gets stuck in a loop for a while. Until this loop exits,
there is no-one looking at incoming event messages any more. This
means that your app will not respond to things like the window being
moved or uncovered, or any mouse-clicks on any "Cancel" button.

Also, the act of setting a progress-bar value does not repaint the
bar, it just places a repaint request on the event message queue 
(which we already know no-one is reading any more). As you discovered,
this means the progress bar does not get repainted until the loop exits
and releases the event thread to look at the message queue again.

The only solution is to run the loop in a separate thread: The event
thread can call the button-click handler where you start a new thread
and quickly return. Then the event thread is free to handle repaint
requests and Cancel button clicks while the new thread plods along 
doing its lengthy work. 

I would always advise doing a job that takes significant time in a 
separate thread.

Steve.

Matt Stevens wrote:
> 
> Hi all,
> 
> I can not seem to update a  progressBar in Real-Time.  I wrote a simple
> app (below) to show the problem.  I put a PB.setValue() inside a FOR
> loop, the the PB doesn't update until the FOR loop finishes.  Does
> anyone know what the problems is?
> 
> Matt
Technical Details
Message-ID: <39478203.94FE0D9A@cwcom.co.uk>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <394673A9.F203FA29@mmm.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,502ec09032bb7e80
X-Google-Attributes: gid111d45,public
From: Steve Horsley <steve.horsley@cwcom.co.uk>
Subject: Re: ProgressBar doesn't update until FOR loop ends?
Date: 2000/06/14
Message-ID: <39478203.94FE0D9A@cwcom.co.uk>
X-Deja-AN: 634419466
Content-Transfer-Encoding: 7bit
References: <394673A9.F203FA29@mmm.com>
X-Accept-Language: en
Content-Type: text/plain; charset=us-ascii
X-Trace: 14 Jun 2000 05:51:25 -0800, 127.0.0.1
MIME-Version: 1.0
Newsgroups: borland.public.jbuilder.java.swing