Why is EventListenerList traversed backwards in fireFooXXX()?

Posted by Joonas Pulakka on Stack Overflow See other posts from Stack Overflow or by Joonas Pulakka
Published on 2009-10-13T10:52:18Z Indexed on 2010/05/31 4:42 UTC
Read the original article Hit count: 251

Filed under:
|
|

I don't understand the rationale of this code, taken from javax.swing.event.EventListenerList docs:

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);
        }
    }
}
  1. Why is the list traversed backwards?
  2. Why is only every second listener called?

The event firing is implemented exactly this way in javax.swing.tree.DefaultTreeModel among others, so it's obviously me who's just not getting something.

© Stack Overflow or respective owner

Related posts about swing

Related posts about jdk