# Class EventDispatcher

java.lang.Object
com.codename1.ui.util.EventDispatcher

public class EventDispatcher extends Object

Handles event dispatching while guaranteeing that all events would be fired properly on the EDT regardless of their source. This class handles listener registration/removal in a safe and uniform way.

To integrate this into your code you can use something like:

    private final EventDispatcher listeners = new EventDispatcher();

    public void addActionListener(ActionListener a) {
        listeners.addListener(a);
    }
    public void removeActionListener(ActionListener a) {
        listeners.removeListener(a);
    }
    private void fireEvent(ActionEvent ev) {
        listeners.fireActionEvent(ev);
    }

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `EventDispatcher()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `addListener(Object listener)`
  Add a listener to the dispatcher that would receive the events when they occurs
  `void`
  `fireActionEvent(ActionEvent ev)`
  Fires the event safely on the EDT without risk of concurrency errors
  `void`
  `fireBindTargetChange(Component source, String propertyName, Object oldValue, Object newValue)`
  Fired when a property of the component changes to a new value
  `void`
  `fireDataChangeEvent(int index, int type)`
  Fires the event safely on the EDT without risk of concurrency errors
  `void`
  `fireFocus(Component c)`
  Fires the event safely on the EDT without risk of concurrency errors
  `void`
  `fireScrollEvent(int scrollX, int scrollY, int oldscrollX, int oldscrollY)`
  Fires the event safely on the EDT without risk of concurrency errors
  `void`
  `fireSelectionEvent(int oldSelection, int newSelection)`
  Fires the event safely on the EDT without risk of concurrency errors
  `void`
  `fireStyleChangeEvent(String property, Style source)`
  Fires the style change even to the listeners
  `Collection`
  `getListenerCollection()`
  Returns the collection of the listeners
  `Vector`
  `getListenerVector()`
  Returns the vector of the listeners
  `boolean`
  `hasListeners()`
  Returns true if the event dispatcher has registered listeners
  `boolean`
  `isBlocking()`
  Indicates whether this dispatcher blocks when firing events or not, normally a dispatcher uses callSeriallyAndWait() to be 100% synchronous with event delivery however this method is very slow.
  `void`
  `removeListener(Object listener)`
  Remove the listener from the dispatcher
  `void`
  `setBlocking(boolean blocking)`
  Indicates whether this dispatcher blocks when firing events or not, normally a dispatcher uses callSeriallyAndWait() to be 100% synchronous with event delivery however this method is very slow.
  `static void`
  `setFireStyleEventsOnNonEDT(boolean fire)`
  When set to true, style events will be dispatched even from non-EDT threads.

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### EventDispatcher

    public EventDispatcher()

- ## Method Details

  - ### setFireStyleEventsOnNonEDT

    public static void setFireStyleEventsOnNonEDT(boolean fire)

    When set to true, style events will be dispatched even from non-EDT threads. When set to false, when in non-EDT threads, style events will not be dispatched at all (And developer has to make sure changes will be reflected by calling revalidate after all the changes)

    Default is false. Setting this to true results in a performance penalty, and it is better instead to simply aggregate events performed on non-EDT threads and when all are over - call revalidate on the relevant container.

    ###### Parameters

    - `fire`: true to fire on non-EDT, false otherwise

  - ### addListener

    public void addListener(Object listener)

    Add a listener to the dispatcher that would receive the events when they occurs

    ###### Parameters

    - `listener`: a dispatcher listener to add

  - ### getListenerVector

    public Vector getListenerVector()

    Returns the vector of the listeners

    ###### Returns

    the vector of listeners attached to the event dispatcher

    ###### Deprecated

    use getListenerCollection instead, this method will now be VERY SLOW

  - ### getListenerCollection

    public Collection getListenerCollection()

    Returns the collection of the listeners

    ###### Returns

    the collection of listeners attached to the event dispatcher

  - ### removeListener

    public void removeListener(Object listener)

    Remove the listener from the dispatcher

    ###### Parameters

    - `listener`: a dispatcher listener to remove

  - ### fireDataChangeEvent

    public void fireDataChangeEvent(int index, int type)

    Fires the event safely on the EDT without risk of concurrency errors

    ###### Parameters

    - `index`: the index of the event

    - `type`: the type of the event

  - ### fireBindTargetChange

    public void fireBindTargetChange(Component source, String propertyName, Object oldValue, Object newValue)

    Fired when a property of the component changes to a new value

    ###### Parameters

    - `source`: the source component

    - `propertyName`: the name of the property

    - `oldValue`: the old value of the property

    - `newValue`: the new value for the property

  - ### fireStyleChangeEvent

    public void fireStyleChangeEvent(String property, Style source)

    Fires the style change even to the listeners

    ###### Parameters

    - `property`: the property name for the event

    - `source`: the style firing the event

  - ### fireActionEvent

    public void fireActionEvent(ActionEvent ev)

    Fires the event safely on the EDT without risk of concurrency errors

    ###### Parameters

    - `ev`: the ActionEvent to fire to the listeners

  - ### fireSelectionEvent

    public void fireSelectionEvent(int oldSelection, int newSelection)

    Fires the event safely on the EDT without risk of concurrency errors

    ###### Parameters

    - `oldSelection`: old selection

    - `newSelection`: new selection

  - ### fireScrollEvent

    public void fireScrollEvent(int scrollX, int scrollY, int oldscrollX, int oldscrollY)
    Fires the event safely on the EDT without risk of concurrency errors

  - ### fireFocus

    public void fireFocus(Component c)

    Fires the event safely on the EDT without risk of concurrency errors

    ###### Parameters

    - `c`: the Component that gets the focus event

  - ### hasListeners

    public boolean hasListeners()

    Returns true if the event dispatcher has registered listeners

    ###### Returns

    true if the event dispatcher has registered listeners

  - ### isBlocking

    public boolean isBlocking()

    Indicates whether this dispatcher blocks when firing events or not, normally a dispatcher uses callSeriallyAndWait() to be 100% synchronous with event delivery however this method is very slow. By setting blocking to false the callSerially method is used which allows much faster execution for IO heavy operations.

    ###### Returns

    the blocking state

  - ### setBlocking

    public void setBlocking(boolean blocking)

    Indicates whether this dispatcher blocks when firing events or not, normally a dispatcher uses callSeriallyAndWait() to be 100% synchronous with event delivery however this method is very slow. By setting blocking to false the callSerially method is used which allows much faster execution for IO heavy operations.

    ###### Parameters

    - `blocking`: the blocking value