Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>1.</h2> <p>One possible problem when traversing listeners is described in <a href="http://amzn.com/0596009070" rel="nofollow noreferrer">Swing Hacks</a> item #94, and happens if one of them removes itself as a listener in it's implementation of fooXXX().</p> <p>Consider this listener, that might remove itself after receiving the event:</p> <pre><code>public class FooListener implements EventListener { private int i; public FooListener(int i) { this.i = i; } public fooXXX(FooEvent foo) { System.out.println(i); if (i == 1) { ((FooEventSource)foo.getSource()).removeListener(this); } } } </code></pre> <p>and this implementation of the listener traversal:</p> <pre><code>public void fireFooXXX() { for (int i=0; i&lt;listeners.size(); i++) { // Lazily create the event: if (fooEvent == null) fooEvent = new FooEvent(this); listeners.get(i).fooXXX(fooEvent); } } </code></pre> <p>Now suppose we create a number of these listeners: </p> <pre><code>fooEventSource.addListener(new FooListener(0)); fooEventSource.addListener(new FooListener(1)); fooEventSource.addListener(new FooListener(2)); fooEventSource.addListener(new FooListener(3)); </code></pre> <p>Firing the event would give the following output:</p> <pre><code>0 1 3 </code></pre> <p>We would be looping over the listeners by index, from 0 to 3. At index 1 the listener removes itself from the internal array of listeners, causing listeners 2 and 3 to be shifted down to index 1 and 2. The loop continues with index 2 which now contains listener 3. Listener 2 has been skipped. </p> <p>By iterating backwards this problem is eliminated since removing a listener would only shift the index of listeners that have already been called.</p> <p><strong>But</strong> </p> <p>EventListenerList does not have this problem, since the add() and remove() methods are copy-on-write and the listener traversal in the suggested usage operates on the listener list instance returned by getListenerList() before the loop.</p> <p>Some more discussions about it can be found in <a href="http://www.java.net/node/659148" rel="nofollow noreferrer">this thread</a>, where the reasons seems to boil down to one of:</p> <ul> <li><p>performance</p></li> <li><p>event ordering (the last added listener will be the first to be notified)</p></li> </ul> <hr> <h2>2.</h2> <p>akf and Michael Borgwardt has already answered that the EvenListenerList stores the listener types in addition to the listeners. I guess the reason for this is that it makes it possible for a single EventListenerList to handle listeners of different types.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload