
478
|
Chapter 12, Miscellany
#95 Debug Your GUI
HACK
Notice the odd for loop—you start at the second-to-last element and decre-
ment by two each time. On each pass, you take the first object of the pair; if
it’s the right class to fire the event to, you get the second object of the pair
and call its event-handling method. Presumably, you could use this to mix
up different kinds of listeners in the same list, calling different methods for
different classes.
When run, this test produces the following output. Since the event firing
counts down through the listeners, it’s functionally the same as the simpler
backward
for
loop shown earlier:
[tonberry] cadamson% java EventListenerListEventSource
E called
D called
C called
B called
A called
H A C K
#95
Debug Your GUI Hack #95
Standard out and err aren’t just for log files anymore.
Debugging GUIs often means keeping one or two console windows open, so
you can see the debugging messages you print to standard out (via
System.
out.println( )
and the like), as well as stack traces printed to standard err
when exceptions are caught. In the field, you might want to log these to a
file with something like
java.util.logging, but at design time, or when
investigating a bug, you want to see exactly when the exceptions happen,
and running
tail -f mylog.txt in multiple terminal windows may not be
practical, especially if you’re trying to get a customer on the phone to do it.
An