add JavaFX Draggable Node - Java JavaFX

Java examples for JavaFX:Node

Description

add JavaFX Draggable Node

Demo Code


    //package com.java2s;

    import javafx.scene.Node;
    import javafx.scene.input.MouseEvent;

    public class Main {
        static double dragInitialX;
        static double dragInitialY;

        public static void addDraggableNode(final Node node) {

    node.setOnMousePressed((MouseEvent me) -> {
        dragInitialX = me.getSceneX();/*  www  .j  a  va 2s .c om*/
        dragInitialY = me.getSceneY();
    });

    node.setOnMouseDragged((MouseEvent me) -> {
        node.getScene().getWindow().setX(me.getScreenX() - dragInitialX);
        node.getScene().getWindow().setY(me.getScreenY() - dragInitialY);
    });
}
    }

Related Tutorials