Article View: borland.public.jbuilder.java.swing
Article #10123Re: Swing, Threads, and Progress meters.
From: Tad Frysinger
Date: Tue, 09 Nov 1999 00:00
Date: Tue, 09 Nov 1999 00:00
62 lines
3800 bytes
3800 bytes
I agree with (and use) Steve's approach. I typically write functional units as beans. These beans communicate their status (including status that would normally update a GUI control such as a progress bar or status bar) via events. One central event handler deals with updates that it listens for, and uses the invokeLater() technique. For your model updates, I'd route them all throught the same (or similar) handler, and use invokeLater() there as well. Take a look at the SwingWorker class at the JavaSoft site (Articles on its use are at www.theswingconnection.com). In terms of you wanting to cancel an operation - be sure that you properly structure your code so that you can issue an InterruptedException that will be dealt with appropriately (note that Thread.interrupt() doesn't really do anything on any of the platforms thus far, except set a bit that the thread was requested to be interrupted). Tad Frysinger (TeamB) "John P. McGrath" wrote: > I have a Swing desktop application that is currently written without any > explicit thread creation. Most of the operations that the program > performs are quite simple and take little time. However, there is an > operation that can be rather lengthy -- on the order of 5 seconds, but > with complex data it might run up to 20 or 30 seconds. > > Currently, the GUI is pretty much dead while this operation is ongoing. > The fact that it does not respond to user input is a concern, but it is > not critical since there is little that the user would be allowed to do > while this is running. However, it would be useful to give the user the > option of canceling. > > But my primary concern is that the GUI does not _look_ like it is doing > anything. To solve this problem, I would like to show a ProgressMeter > while the operation is performed, but I am not sure of the best way to do > this. I am hoping that the solution will also provide the capability of > canceling the operation. > > The ProgressMeter / ProgressMonitor examples I have seen all perform the > long task in a separate thread, and my application currently does all of > its work in the event-dispatch thread. I am concerned that moving this > work to a separate thread may cause problems. > > The application uses a Model-View architecture. The Swing view > components are registered as listeners on the model objects, which use > javax.swing.event.EventListenerList for registering listeners and firing > events. All GUI updates are triggered through changes to the model. > > The long operation perform extensive modifications of the model, which > results in many events fired at the Swing view components, which causes > these components to change their appearance, create some new components > and remove others, etc. This works fine, since it is all happening from > the event-dispatch thread. But if I were to create a separate thread to > perform the operation, this would violate the Swing single-thread rule. > > Does anyone have any comments or suggestions as how to deal with this? > > -- > John P. McGrath > <mcgrath@enter.net> > <jpm@nscs.fast.net>
Technical Details
Message-ID:
<38283638.2127196E@earthlink.net>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<MPG.1289bc46f1d0f7aa989685@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,eebaae9990e98781 X-Google-Attributes: gid111d45,public From: Tad Frysinger <tfrysinger@earthlink.net> Subject: Re: Swing, Threads, and Progress meters. Date: 1999/11/09 Message-ID: <38283638.2127196E@earthlink.net> X-Deja-AN: 546463560 Content-Transfer-Encoding: 7bit References: <MPG.1289bc46f1d0f7aa989685@forums.borland.com> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii Organization: Another Netscape Collabra Server User Mime-Version: 1.0 Reply-To: tfrysinger@earthlink.net Newsgroups: borland.public.jbuilder.java.swing