Skip to content

Latest commit

 

History

History
42 lines (34 loc) · 1.03 KB

README.md

File metadata and controls

42 lines (34 loc) · 1.03 KB

GenericEventListeners for Java

Usage

  1. Derive your class from Listenable
class MyClass extends Listenable {
}
  1. Use addActionListener() for registering for an event type and implement the Listener interface.
class Main {
      public static void main(String[] args) {
        MyClass myObj = new MyClass();
        myObj.addEventListener("dispatched", new Listener<String>() {
            public boolean run(Event<String> e) {
              System.out.println(e.data);
            }
        });
        myObj.doDispatch();
      }
}
  1. Dispatch an event from your Listenable class:
class MyClass extends Listenable {
      public void doDispatch() {
        dispatchEvent( new Event<String>("dispatched", "Hello World!") );
      }
}

Warning: dispatchEvent() does not check whether the type of the dispatched Event (here String) and the registered listeners match!

About

Author: @comfreek
License: MIT license, see LICENSE file for more information.