title: JavaFX. Register eventHandler in custom class
body: <p>I try register eventHandler in my custom class. I don't know what interface or methods I have to implement for having <code>addEventHandler</code> method in my custom class. For this reason my <code>Model</code> class extends <code>Rectangle</code> (<code>Rectangle</code> class has <code>addEventHandler</code> mechanism).</p>
  
  <p>Also I don't know why assigned <strong>source object</strong> not working (please see comment in <code>Controller</code> class).</p>
  
  <p>Creating custom events I make by this tutorial: <a href="https://stackoverflow.com/a/27423430/3102393">https://stackoverflow.com/a/27423430/3102393</a>.</p>
  
  <hr>
  
  <p><strong>Project Structure</strong></p>
  
  <p><em>Controller</em></p>
  
  <pre><code>package sample;
  
  import javafx.event.Event;
  
  public class Controller {
      private Model model;
  
      public Controller() {
          model = new Model();
  
          model.addEventHandler(MyEvent.ROOT_EVENT, this::handler);
      }
  
      private void handler(MyEvent event) {
          if(event.getEventType().equals(MyEvent.INSTANCE_CREATED)) {
              // Why is event.getSource() instence of Rectangle and not instance of assigned MyObject?
              Object obj = event.getSource();
              System.out.println(event.getMyObject().getText());
          }
      }
  
      public void clickedCreate(Event event) {
          model.makeEvent();
      }
  }
  </code></pre>
  
  <p><em>Model</em></p>
  
  <pre><code>package sample;
  
  import javafx.scene.shape.Rectangle;
  import java.util.ArrayList;
  
  public class Model extends Rectangle {
  
      private ArrayList&lt;MyObject&gt; objects = new ArrayList&lt;&gt;();
      private Integer counter = 0;
  
      public void makeEvent() {
          MyObject object = new MyObject((++counter).toString() + "!");
  
          objects.add(object);
          fireEvent(new MyEvent(object, null, MyEvent.INSTANCE_CREATED));
      }
  }
  </code></pre>
  
  <p>Custom event <em>MyEvent</em></p>
  
  <pre><code>package sample;
  
  import javafx.event.Event;
  import javafx.event.EventTarget;
  import javafx.event.EventType;
  
  public class MyEvent extends Event {
  
      public static final EventType&lt;MyEvent&gt; ROOT_EVENT = new EventType&lt;&gt;(Event.ANY, "ROOT_EVENT");
      public static final EventType&lt;MyEvent&gt; INSTANCE_CREATED = new EventType&lt;&gt;(ROOT_EVENT, "INSTANCE_CREATED ");
      public static final EventType&lt;MyEvent&gt; INSTANCE_DELETED = new EventType&lt;&gt;(ROOT_EVENT, "INSTANCE_DELETED");
  
      private MyObject object;
  
      public MyEvent(MyObject source, EventTarget target, EventType&lt;MyEvent&gt; eventType) {
          super(source, target, eventType);
  
          object = source;
      }
  
      public MyObject getMyObject() {
          return object;
      }
  }
  </code></pre>
  
  <p>And finally MyObject</p>
  
  <pre><code>package sample;
  
  public class MyObject {
      private String text;
  
      MyObject(String text) {
          this.text = text;
      }
  
      public String getText() {
          return text;
      }
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Note (and question)</strong>: I also tried using a <code>ObservableList</code> of instances of <code>MyObject</code>s, but I think that there is <strong>no</strong> notify for updating instance attribute.</p>