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

Get Swing Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.