public class AWTEventMulticaster
extends Object
implements ComponentListener, ContainerListener, FocusListener, KeyListener, MouseListener, MouseMotionListener, WindowListener, WindowFocusListener, WindowStateListener, ActionListener, ItemListener, AdjustmentListener, TextListener, InputMethodListener, HierarchyListener, HierarchyBoundsListener, MouseWheelListener

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 {@code 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 {@code EventListeners} that are set at construction time. {@code AWTEventMulticaster} is immutable. The add and {@code 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 javax.swing.event.EventListenerList EventListenerList for similar logic. Refer to it for details.

protected final a

protected final b

protected AWTEventMulticaster(EventListener, EventListener)

Creates an event multicaster instance which chains listener-a with listener-b. Input parameters a and b should not be null, though implementations may vary in choosing whether or not to throw NullPointerException in that case.

Parameters

EventListener a: listener-a

EventListener b: listener-b

public void actionPerformed(ActionEvent)

Handles the actionPerformed event by invoking the actionPerformed methods on listener-a and listener-b.

Parameters

ActionEvent e: the action event

public static ActionListener add(ActionListener, ActionListener)

Adds action-listener-a with action-listener-b and returns the resulting multicast listener.

Parameters

ActionListener a: action-listener-a

ActionListener b: action-listener-b

public static AdjustmentListener add(AdjustmentListener, AdjustmentListener)

Adds adjustment-listener-a with adjustment-listener-b and returns the resulting multicast listener.

Parameters

AdjustmentListener a: adjustment-listener-a

AdjustmentListener b: adjustment-listener-b

public static ComponentListener add(ComponentListener, ComponentListener)

Adds component-listener-a with component-listener-b and returns the resulting multicast listener.

Parameters

ComponentListener a: component-listener-a

ComponentListener b: component-listener-b

public static ContainerListener add(ContainerListener, ContainerListener)

Adds container-listener-a with container-listener-b and returns the resulting multicast listener.

Parameters

ContainerListener a: container-listener-a

ContainerListener b: container-listener-b

public static FocusListener add(FocusListener, FocusListener)

Adds focus-listener-a with focus-listener-b and returns the resulting multicast listener.

Parameters

FocusListener a: focus-listener-a

FocusListener b: focus-listener-b

public static HierarchyBoundsListener add(HierarchyBoundsListener, HierarchyBoundsListener)

Adds hierarchy-bounds-listener-a with hierarchy-bounds-listener-b and returns the resulting multicast listener.

Parameters

HierarchyBoundsListener a: hierarchy-bounds-listener-a

HierarchyBoundsListener b: hierarchy-bounds-listener-b

public static HierarchyListener add(HierarchyListener, HierarchyListener)

Adds hierarchy-listener-a with hierarchy-listener-b and returns the resulting multicast listener.

Parameters

HierarchyListener a: hierarchy-listener-a

HierarchyListener b: hierarchy-listener-b

public static InputMethodListener add(InputMethodListener, InputMethodListener)

Adds input-method-listener-a with input-method-listener-b and returns the resulting multicast listener.

Parameters

InputMethodListener a: input-method-listener-a

InputMethodListener b: input-method-listener-b

public static ItemListener add(ItemListener, ItemListener)

Adds item-listener-a with item-listener-b and returns the resulting multicast listener.

Parameters

ItemListener a: item-listener-a

ItemListener b: item-listener-b

public static KeyListener add(KeyListener, KeyListener)

Adds key-listener-a with key-listener-b and returns the resulting multicast listener.

Parameters

KeyListener a: key-listener-a

KeyListener b: key-listener-b

public static MouseListener add(MouseListener, MouseListener)

Adds mouse-listener-a with mouse-listener-b and returns the resulting multicast listener.

Parameters

MouseListener a: mouse-listener-a

MouseListener b: mouse-listener-b

public static MouseMotionListener add(MouseMotionListener, MouseMotionListener)

Adds mouse-motion-listener-a with mouse-motion-listener-b and returns the resulting multicast listener.

Parameters

MouseMotionListener a: mouse-motion-listener-a

MouseMotionListener b: mouse-motion-listener-b

public static MouseWheelListener add(MouseWheelListener, MouseWheelListener)

Adds mouse-wheel-listener-a with mouse-wheel-listener-b and returns the resulting multicast listener.

Parameters

MouseWheelListener a: mouse-wheel-listener-a

MouseWheelListener b: mouse-wheel-listener-b

public static TextListener add(TextListener, TextListener)

Parameters

public static WindowFocusListener add(WindowFocusListener, WindowFocusListener)

Adds window-focus-listener-a with window-focus-listener-b and returns the resulting multicast listener.

Parameters

WindowFocusListener a: window-focus-listener-a

WindowFocusListener b: window-focus-listener-b

public static WindowListener add(WindowListener, WindowListener)

Adds window-listener-a with window-listener-b and returns the resulting multicast listener.

Parameters

WindowListener a: window-listener-a

WindowListener b: window-listener-b

public static WindowStateListener add(WindowStateListener, WindowStateListener)

Adds window-state-listener-a with window-state-listener-b and returns the resulting multicast listener.

Parameters

WindowStateListener a: window-state-listener-a

WindowStateListener b: window-state-listener-b

protected static EventListener addInternal(EventListener, EventListener)

Returns the resulting multicast listener from adding listener-a and listener-b together. If listener-a is null, it returns listener-b; If listener-b is null, it returns listener-a If neither are null, then it creates and returns a new AWTEventMulticaster instance which chains a with b.

Parameters

EventListener a: event listener-a

EventListener b: event listener-b

public void adjustmentValueChanged(AdjustmentEvent)

Handles the adjustmentValueChanged event by invoking the adjustmentValueChanged methods on listener-a and listener-b.

Parameters

AdjustmentEvent e: the adjustment event

public void ancestorMoved(HierarchyEvent)

Handles the ancestorMoved event by invoking the ancestorMoved methods on listener-a and listener-b.

Parameters

HierarchyEvent e: the item event

public void ancestorResized(HierarchyEvent)

Handles the ancestorResized event by invoking the ancestorResized methods on listener-a and listener-b.

Parameters

HierarchyEvent e: the item event

public void caretPositionChanged(InputMethodEvent)

Handles the caretPositionChanged event by invoking the caretPositionChanged methods on listener-a and listener-b.

Parameters

InputMethodEvent e: the item event

public void componentAdded(ContainerEvent)

Handles the componentAdded container event by invoking the componentAdded methods on listener-a and listener-b.

Parameters

ContainerEvent e: the component event

public void componentHidden(ComponentEvent)

Handles the componentHidden event by invoking the componentHidden methods on listener-a and listener-b.

Parameters

ComponentEvent e: the component event

public void componentMoved(ComponentEvent)

Handles the componentMoved event by invoking the componentMoved methods on listener-a and listener-b.

Parameters

ComponentEvent e: the component event

public void componentRemoved(ContainerEvent)

Handles the componentRemoved container event by invoking the componentRemoved methods on listener-a and listener-b.

Parameters

ContainerEvent e: the component event

public void componentResized(ComponentEvent)

Handles the componentResized event by invoking the componentResized methods on listener-a and listener-b.

Parameters

ComponentEvent e: the component event

public void componentShown(ComponentEvent)

Handles the componentShown event by invoking the componentShown methods on listener-a and listener-b.

Parameters

ComponentEvent e: the component event

public void focusGained(FocusEvent)

Handles the focusGained event by invoking the focusGained methods on listener-a and listener-b.

Parameters

FocusEvent e: the focus event

public void focusLost(FocusEvent)

Handles the focusLost event by invoking the focusLost methods on listener-a and listener-b.

Parameters

FocusEvent e: the focus event

public static EventListener getListeners(EventListener, Class)

Returns an array of all the objects chained as FooListeners by the specified java.util.EventListener. FooListeners are chained by the AWTEventMulticaster using the addFooListener method. If a null listener is specified, this method returns an empty array. If the specified listener is not an instance of AWTEventMulticaster, this method returns an array which contains only the specified listener. If no such listeners are chanined, this method returns an empty array.

Parameters

EventListener l: the specified java.util.EventListener

java.lang.Class listenerType: the type of listeners requested; this parameter should specify an interface that descends from java.util.EventListener

Throws

NullPointerException: if the specified listenertype parameter is null

ClassCastException: if listenerType doesn't specify a class or interface that implements java.util.EventListener

public void hierarchyChanged(HierarchyEvent)

Handles the hierarchyChanged event by invoking the hierarchyChanged methods on listener-a and listener-b.

Parameters

HierarchyEvent e: the item event

public void inputMethodTextChanged(InputMethodEvent)

Handles the inputMethodTextChanged event by invoking the inputMethodTextChanged methods on listener-a and listener-b.

Parameters

InputMethodEvent e: the item event

public void itemStateChanged(ItemEvent)

Handles the itemStateChanged event by invoking the itemStateChanged methods on listener-a and listener-b.

Parameters

ItemEvent e: the item event

public void keyPressed(KeyEvent)

Handles the keyPressed event by invoking the keyPressed methods on listener-a and listener-b.

Parameters

KeyEvent e: the key event

public void keyReleased(KeyEvent)

Handles the keyReleased event by invoking the keyReleased methods on listener-a and listener-b.

Parameters

KeyEvent e: the key event

public void keyTyped(KeyEvent)

Handles the keyTyped event by invoking the keyTyped methods on listener-a and listener-b.

Parameters

KeyEvent e: the key event

public void mouseClicked(MouseEvent)

Handles the mouseClicked event by invoking the mouseClicked methods on listener-a and listener-b.

Parameters

MouseEvent e: the mouse event

public void mouseDragged(MouseEvent)

Handles the mouseDragged event by invoking the mouseDragged methods on listener-a and listener-b.

Parameters

MouseEvent e: the mouse event

public void mouseEntered(MouseEvent)

Handles the mouseEntered event by invoking the mouseEntered methods on listener-a and listener-b.

Parameters

MouseEvent e: the mouse event

public void mouseExited(MouseEvent)

Handles the mouseExited event by invoking the mouseExited methods on listener-a and listener-b.

Parameters

MouseEvent e: the mouse event

public void mouseMoved(MouseEvent)

Handles the mouseMoved event by invoking the mouseMoved methods on listener-a and listener-b.

Parameters

MouseEvent e: the mouse event

public void mousePressed(MouseEvent)

Handles the mousePressed event by invoking the mousePressed methods on listener-a and listener-b.

Parameters

MouseEvent e: the mouse event

public void mouseReleased(MouseEvent)

Handles the mouseReleased event by invoking the mouseReleased methods on listener-a and listener-b.

Parameters

MouseEvent e: the mouse event

public void mouseWheelMoved(MouseWheelEvent)

Handles the mouseWheelMoved event by invoking the mouseWheelMoved methods on listener-a and listener-b.

Parameters

MouseWheelEvent e: the mouse event

public static ActionListener remove(ActionListener, ActionListener)

Removes the old action-listener from action-listener-l and returns the resulting multicast listener.

Parameters

ActionListener l: action-listener-l

ActionListener oldl: the action-listener being removed

public static AdjustmentListener remove(AdjustmentListener, AdjustmentListener)

Removes the old adjustment-listener from adjustment-listener-l and returns the resulting multicast listener.

Parameters

AdjustmentListener l: adjustment-listener-l

AdjustmentListener oldl: the adjustment-listener being removed

public static ComponentListener remove(ComponentListener, ComponentListener)

Removes the old component-listener from component-listener-l and returns the resulting multicast listener.

Parameters

ComponentListener l: component-listener-l

ComponentListener oldl: the component-listener being removed

public static ContainerListener remove(ContainerListener, ContainerListener)

Removes the old container-listener from container-listener-l and returns the resulting multicast listener.

Parameters

ContainerListener l: container-listener-l

ContainerListener oldl: the container-listener being removed

protected EventListener remove(EventListener)

Removes a listener from this multicaster.

The returned multicaster contains all the listeners in this multicaster with the exception of all occurrences of oldl. If the resulting multicaster contains only one regular listener the regular listener may be returned. If the resulting multicaster is empty, then null may be returned instead.

No exception is thrown if oldl is null.

Parameters

EventListener oldl: the listener to be removed

public static FocusListener remove(FocusListener, FocusListener)

Removes the old focus-listener from focus-listener-l and returns the resulting multicast listener.

Parameters

FocusListener l: focus-listener-l

FocusListener oldl: the focus-listener being removed

public static HierarchyBoundsListener remove(HierarchyBoundsListener, HierarchyBoundsListener)

Removes the old hierarchy-bounds-listener from hierarchy-bounds-listener-l and returns the resulting multicast listener.

Parameters

HierarchyBoundsListener l: hierarchy-bounds-listener-l

HierarchyBoundsListener oldl: the hierarchy-bounds-listener being removed

public static HierarchyListener remove(HierarchyListener, HierarchyListener)

Removes the old hierarchy-listener from hierarchy-listener-l and returns the resulting multicast listener.

Parameters

HierarchyListener l: hierarchy-listener-l

HierarchyListener oldl: the hierarchy-listener being removed

public static InputMethodListener remove(InputMethodListener, InputMethodListener)

Removes the old input-method-listener from input-method-listener-l and returns the resulting multicast listener.

Parameters

InputMethodListener l: input-method-listener-l

InputMethodListener oldl: the input-method-listener being removed

public static ItemListener remove(ItemListener, ItemListener)

Removes the old item-listener from item-listener-l and returns the resulting multicast listener.

Parameters

ItemListener l: item-listener-l

ItemListener oldl: the item-listener being removed

public static KeyListener remove(KeyListener, KeyListener)

Removes the old key-listener from key-listener-l and returns the resulting multicast listener.

Parameters

KeyListener l: key-listener-l

KeyListener oldl: the key-listener being removed

public static MouseListener remove(MouseListener, MouseListener)

Removes the old mouse-listener from mouse-listener-l and returns the resulting multicast listener.

Parameters

MouseListener l: mouse-listener-l

MouseListener oldl: the mouse-listener being removed

public static MouseMotionListener remove(MouseMotionListener, MouseMotionListener)

Removes the old mouse-motion-listener from mouse-motion-listener-l and returns the resulting multicast listener.

Parameters

MouseMotionListener l: mouse-motion-listener-l

MouseMotionListener oldl: the mouse-motion-listener being removed

public static MouseWheelListener remove(MouseWheelListener, MouseWheelListener)

Removes the old mouse-wheel-listener from mouse-wheel-listener-l and returns the resulting multicast listener.

Parameters

MouseWheelListener l: mouse-wheel-listener-l

MouseWheelListener oldl: the mouse-wheel-listener being removed

public static TextListener remove(TextListener, TextListener)

Parameters

public static WindowFocusListener remove(WindowFocusListener, WindowFocusListener)

Removes the old window-focus-listener from window-focus-listener-l and returns the resulting multicast listener.

Parameters

WindowFocusListener l: window-focus-listener-l

WindowFocusListener oldl: the window-focus-listener being removed

public static WindowListener remove(WindowListener, WindowListener)

Removes the old window-listener from window-listener-l and returns the resulting multicast listener.

Parameters

WindowListener l: window-listener-l

WindowListener oldl: the window-listener being removed

public static WindowStateListener remove(WindowStateListener, WindowStateListener)

Removes the old window-state-listener from window-state-listener-l and returns the resulting multicast listener.

Parameters

WindowStateListener l: window-state-listener-l

WindowStateListener oldl: the window-state-listener being removed

protected static EventListener removeInternal(EventListener, EventListener)

Returns the resulting multicast listener after removing the old listener from listener-l. If listener-l equals the old listener OR listener-l is null, returns null. Else if listener-l is an instance of AWTEventMulticaster, then it removes the old listener from it. Else, returns listener l.

Parameters

EventListener l: the listener being removed from

EventListener oldl: the listener being removed

protected static void save(ObjectOutputStream, String, EventListener)

Parameters

protected void saveInternal(ObjectOutputStream, String)

Parameters

public void textValueChanged(TextEvent)

Parameters

public void windowActivated(WindowEvent)

Handles the windowActivated event by invoking the windowActivated methods on listener-a and listener-b.

Parameters

WindowEvent e: the window event

public void windowClosed(WindowEvent)

Handles the windowClosed event by invoking the windowClosed methods on listener-a and listener-b.

Parameters

WindowEvent e: the window event

public void windowClosing(WindowEvent)

Handles the windowClosing event by invoking the windowClosing methods on listener-a and listener-b.

Parameters

WindowEvent e: the window event

public void windowDeactivated(WindowEvent)

Handles the windowDeactivated event by invoking the windowDeactivated methods on listener-a and listener-b.

Parameters

WindowEvent e: the window event

public void windowDeiconified(WindowEvent)

Handles the windowDeiconfied event by invoking the windowDeiconified methods on listener-a and listener-b.

Parameters

WindowEvent e: the window event

public void windowGainedFocus(WindowEvent)

Handles the windowGainedFocus event by invoking the windowGainedFocus methods on listener-a and listener-b.

Parameters

WindowEvent e: the window event

public void windowIconified(WindowEvent)

Handles the windowIconified event by invoking the windowIconified methods on listener-a and listener-b.

Parameters

WindowEvent e: the window event

public void windowLostFocus(WindowEvent)

Handles the windowLostFocus event by invoking the windowLostFocus methods on listener-a and listener-b.

Parameters

WindowEvent e: the window event

public void windowOpened(WindowEvent)

Handles the windowOpened event by invoking the windowOpened methods on listener-a and listener-b.

Parameters

WindowEvent e: the window event

public void windowStateChanged(WindowEvent)

Handles the windowStateChanged event by invoking the windowStateChanged methods on listener-a and listener-b.

Parameters

WindowEvent e: the window event