Module java.desktop
Package java.awt

Class AWTEventMulticaster

java.lang.Object
java.awt.AWTEventMulticaster
All Implemented Interfaces:
ActionListener, AdjustmentListener, ComponentListener, ContainerListener, FocusListener, HierarchyBoundsListener, HierarchyListener, InputMethodListener, ItemListener, KeyListener, MouseListener, MouseMotionListener, MouseWheelListener, TextListener, WindowFocusListener, WindowListener, WindowStateListener, EventListener

AWTEventMulticaster implements efficient and thread-safe multi-cast event dispatching for the AWT events defined in the java.awt.event package.

The following example illustrates how to use this class:


 public myComponent extends Component {
     ActionListener actionListener = null;

     public synchronized void addActionListener(ActionListener l) {
         actionListener = AWTEventMulticaster.add(actionListener, l);
     }
     public synchronized void removeActionListener(ActionListener l) {
         actionListener = AWTEventMulticaster.remove(actionListener, l);
     }
     public void processEvent(AWTEvent e) {
         // when event occurs which causes "action" semantic
         ActionListener listener = actionListener;
         if (listener != null) {
             listener.actionPerformed(new ActionEvent());
         }
     }
 }
 
The important point to note is the first argument to the add and remove methods is the field maintaining the listeners. In addition you must assign the result of the add and remove methods to the field maintaining the listeners.

AWTEventMulticaster is implemented as a pair of EventListeners that are set at construction time. AWTEventMulticaster is immutable. The add and remove methods do not alter AWTEventMulticaster in anyway. If necessary, a new AWTEventMulticaster is created. In this way it is safe to add and remove listeners during the process of an event dispatching. However, event listeners added during the process of an event dispatch operation are not notified of the event currently being dispatched.

All of the add methods allow null arguments. If the first argument is null, the second argument is returned. If the first argument is not null and the second argument is null, the first argument is returned. If both arguments are non-null, a new AWTEventMulticaster is created using the two arguments and returned.

For the remove methods that take two arguments, the following is returned:

  • null, if the first argument is null, or the arguments are equal, by way of ==.
  • the first argument, if the first argument is not an instance of AWTEventMulticaster.
  • result of invoking remove(EventListener) on the first argument, supplying the second argument to the remove(EventListener) method.

Swing makes use of EventListenerList for similar logic. Refer to it for details.

Since:
1.1
See Also: