Re: Bogus behavior of window listeners
Twisted wrote:
Thomas Hawtin wrote:
It's the only listener you add. It's not necessarily the only listener.
PL&F add listeners. Components add listeners to models. Lots of
listeners are about that you never knew about.
It worked before, so it seems doubtful that this could be the
explanation.
The explanation that Thomas' observation suggests would lead to bugs
where any change in your program or the graphics libraries, even
apparently harmless changes, could matter.
You remove, in effect, an arbitrary Window listener, and then assume
that your listener has been removed.
The API documentation makes no statements about the order of listeners
in the array returned by getWindowListener, so the order could change,
for exactly the same combination of add and remove operations, from
release to release of the software. If your listener happens to be
referenced by element 0, all will be well. If not, you would get exactly
the symptoms you describe, plus possibly additional symptoms. There may
be consequences to the library failing to react to the condition the
removed listener was dealing with.
There is a really simple way to test for this issue. Change:
if (frame.getWindowListeners().length > 0)
frame.removeWindowListener(frame.getWindowListeners()[0]);
to
WindowListener[] listeners = frame.getWindowListeners();
if (listeners.length > 0){
if(listeners.length.length == 1){
frame.removeWindowListener(listeners[0]);
}else{
*** write debug output or throw an exception
}
}
Patricia