Light
Light

Reputation: 185

JavaFX OnMouseDragged

I try create google calendar in JavaFX but i have problem with setOnMouseDragged. My class create calendar event.

public class Events extends Pane {

public Events(Double x, Double y, String name) {

    Rectangle events = new Rectangle();
    events.setWidth(100);
    events.setHeight(50);
    Label label = new Label(name);

    this.getChildren().addAll(events, label);
    this.setLayoutX(x - 40);
    this.setLayoutY(y);

    this.setOnMouseDragged(m -> {
        System.out.println(m.getX() + " " + m.getY());
        this.setLayoutX(m.getX());
        this.setLayoutY(m.getY());

    });
}

And when I drag my event i have this: enter image description here

what's interesting when it changes getX to getSceneX everything is ok. After the logs I see that there is a double stall: After the logs I see that there is a double drag: Dragged 44.0 20.0 Dragged 176.0 29.0

any suggestion ?

Upvotes: 2

Views: 566

Answers (1)

fabian
fabian

Reputation: 82461

Event's x and y properties are coordinates in the coordinate system of the node itself but layoutX and layoutY determine the position in the parent node.

You could transform to parent coordinates to get the desired effect.

Point2D mouseInParent = this.localToParent(m.getX(), m.getY());
this.setLayoutX(mouseInParent.getX());
this.setLayoutY(mouseInParent.getY());

Upvotes: 5

Related Questions