how to make stage dragable in javafx code example

Example: how to make stage dragable in javafx

private double x = 0;
    private double y = 0;

    private void makeCanDrag()	//make sure this method is called at the start.
    {

        parent.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                x = event.getSceneX();
                y = event.getSceneY();
            }
        });

        parent.setOnMouseDragged(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
                stage.setX(event.getSceneX() - x);
                stage.setY(event.getSceneY() - y);
                stage.setOpacity(.8f);
            }
        });
        parent.setOnDragDone(event -> {
            stage.setOpacity(1.0f);
        });
        parent.setOnMouseReleased(event -> {
            stage.setOpacity(1.0f);
        });
    }

Tags:

Misc Example