Swing – why does eventlistenerlist traverse backwards in firefooxxx()?
•
Java
I don't understand the basic principle of this code, which is taken from javax swing. event. Eventlistenerlist document:
protected void fireFooXXX() {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first,notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==FooListener.class) {
// Lazily create the event:
if (fooEvent == null)
fooEvent = new FooEvent(this);
((FooListener)listeners[i+1]).fooXXX(fooEvent);
}
}
}
>Why does the list move backward? > Why is every listener called?
Event fired in javax swing. tree. Defaulttreemodel and so on are implemented in this way, so obviously I just don't get anything
Solution
>Possible performance considerations: backward iterations are faster because comparing with 0 is a single machine code instruction - many former C programmers have deep roots, although it is now quite irrelevant Please note that the order of notification of the audience cannot be guaranteed in any way. > Look at the rest of the class - it also stores the listener's type to provide type security
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
