2

The action is perform when mouse moved in to the object defined in group, coordinates are printed out at which the mouse drops. I found this could be achieved by these two methods:

    // method 1:
    group.setOnMouseMoved(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            System.out.printf("coordinate X: %.2f, coordinate Y: %.2f\n",event.getX(),event.getY());
        }
    });

    // method 2:
    group.onMouseMovedProperty().set(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            System.out.printf("coordinate X: %.2f, coordinate Y: %.2f\n",event.getX(),event.getY());
        }
    });

Both gave me the expected results. The syntax looks almost the same, I just wondered if there's any difference between those two? When do we use one from the other?

GabrielChu
  • 6,026
  • 10
  • 27
  • 42
  • 1
    JavaFX 101. This is the standard for defining javafx properties. The getters and setters are legacy methods to fit the JavaBeans style. You see it all over, not just here. – Mordechai Jun 11 '17 at 05:54
  • Btw, you should be using Java 8 lambdas which makes life damn easier: `group.setOnMouseMoved(evt -> System.out.printf("coordinate X: %.2f, coordinate Y: %.2f\n", evt.getX(), evt.getY()));` – Mordechai Jun 11 '17 at 06:05
  • `onMouseMoved` is a JavaFX property, so the pattern indicates there should be an `onMouseMovedProperty()` method as well as a `getOnMouseMoved()` and a `setOnMouseMoved()` method. See https://stackoverflow.com/questions/33834215/when-to-use-javafx-properties-setter-and-getter-instead-of-using-the-property-d `setOnMouseMoved(x)` does exactly the same as `onMouseMoved().set(x)` – James_D Jun 11 '17 at 14:25

0 Answers0