title: JavaFX: How to extends Rectangle and add an OnMouseClick() handler
body: <p>I want to track how many times a Rectangle is clicked. I decided to extend <code>Rectangle</code> class and add an <code>int</code> variable:</p>
  
  <pre><code>import javafx.scene.paint.Color;
  import javafx.scene.shape.Rectangle;
  
  public class MyRectangle extends Rectangle{
      public int clickedTimes = 0;
      public MyRectangle(int w, int h, Color c) {
          super (w, h, c);
      }   
  }
  </code></pre>
  
  <p>and in the main class i do this:</p>
  
  <pre><code>public class Test extends Application {
  
      GridPane root = new GridPane();
  
      @Override
      public void start(Stage primaryStage) {
          root.setVgap(1);
          root.setHgap(1);
          Scene scene = new Scene(root, 300, 250);
  
          primaryStage.setTitle("Hello World!");
          primaryStage.setScene(scene);
  
          ColumnConstraints cc = new ColumnConstraints();
          cc.setPercentWidth(10);
          RowConstraints rc = new RowConstraints();
          rc.setPercentHeight(10);
          root.getColumnConstraints().add(cc);
          root.getRowConstraints().add(rc);
          for (int c = 0; c &lt; 10; c++) {
              root.getColumnConstraints().add(cc);
              root.getRowConstraints().add(rc);
              for (int r = 0; r &lt; 10; r++) {
                  int w = (int) root.getWidth() / 10;
                  int h = (int) root.getHeight() / 10;
                  final MyRectangle rec;
                  if ((r + c) % 2 == 0) {
                      rec = new MyRectangle(w, h, Color.GRAY);
                  } else {
                      rec = new MyRectangle(w, h, Color.BEIGE);
                  }
                  ((Rectangle)rec).setOnMouseClicked(new Listner(rec)); //****QUESTION IS IN THIS LINE****
                  root.add(rec, c, r);
              }
          }
  
          primaryStage.show();
      }
  ram args the command line arguments
  
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>
  
  <p>The question is: Why do I have to cast <code>rec</code> to <code>(Rectangle)rec</code> in order to get the methiod <code>onMouseClicked()</code> ? MyRectangle should have all methods from the original <code>Rectangle</code> class</p>