O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

472
|
Chapter 12, Miscellany
#94 Fire Events and Stay Bug Free
HACK
H A C K
#94
Fire Events and Stay Bug Free Hack #94
Most developers think that writing an event-firing method is trivial. Most
developers are wrong.
As you develop your own Swing components, it’s likely that you’ll eventu-
ally need to have them fire off events; this comes up as soon as you have a
model that needs to update a view. If you’ve strongly typed everything by
writing new classes for the model, view, event, and listener, then you’ll have
to write your own fire method.
Most developers assume this to be trivial. For example, to manage a list of
FooListeners, they’ll typically maintain a Vector or ArrayList and fire off the
event with a block like this:
Iterator it = listeners.iterator( );
while (it.hasNext( ))
((FooListener).it.next( )).handleEvent (fooEvent);
And there you have it. It’s simple. It’s clean. It’s elegant. It’s wrong.
Figure 12-9. Incremental self-loading with a progress bar

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required