Example usage for javafx.scene Node getTranslateX

List of usage examples for javafx.scene Node getTranslateX

Introduction

In this page you can find the example usage for javafx.scene Node getTranslateX.

Prototype

public final double getTranslateX() 

Source Link

Usage

From source file:Main.java

private Node makeDraggable(final Node node) {
    final DragContext dragContext = new DragContext();
    final Group wrapGroup = new Group(node);

    wrapGroup.addEventFilter(MouseEvent.ANY, new EventHandler<MouseEvent>() {
        public void handle(final MouseEvent mouseEvent) {
            if (dragModeActiveProperty.get()) {
                // disable mouse events for all children
                mouseEvent.consume();/*ww w  .  j  a v  a  2  s. c  o m*/
            }
        }
    });

    wrapGroup.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
        public void handle(final MouseEvent mouseEvent) {
            if (dragModeActiveProperty.get()) {
                // remember initial mouse cursor coordinates
                // and node position
                dragContext.mouseAnchorX = mouseEvent.getX();
                dragContext.mouseAnchorY = mouseEvent.getY();
                dragContext.initialTranslateX = node.getTranslateX();
                dragContext.initialTranslateY = node.getTranslateY();
            }
        }
    });

    wrapGroup.addEventFilter(MouseEvent.MOUSE_DRAGGED, new EventHandler<MouseEvent>() {
        public void handle(final MouseEvent mouseEvent) {
            if (dragModeActiveProperty.get()) {
                // shift node from its initial position by delta
                // calculated from mouse cursor movement
                node.setTranslateX(
                        dragContext.initialTranslateX + mouseEvent.getX() - dragContext.mouseAnchorX);
                node.setTranslateY(
                        dragContext.initialTranslateY + mouseEvent.getY() - dragContext.mouseAnchorY);
            }
        }
    });

    return wrapGroup;
}