how to make stage dragable in javafx

Solutions on MaxInterview for how to make stage dragable in javafx by the best coders in the world

showing results for - "how to make stage dragable in javafx"
Youna
29 Jul 2018
1    private double x = 0;
2    private double y = 0;
3
4    private void makeCanDrag()	//make sure this method is called at the start.
5    {
6
7        parent.setOnMousePressed(new EventHandler<MouseEvent>() {
8            @Override
9            public void handle(MouseEvent event) {
10                x = event.getSceneX();
11                y = event.getSceneY();
12            }
13        });
14
15        parent.setOnMouseDragged(new EventHandler<MouseEvent>() {
16            @Override
17            public void handle(MouseEvent event) {
18                stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
19                stage.setX(event.getSceneX() - x);
20                stage.setY(event.getSceneY() - y);
21                stage.setOpacity(.8f);
22            }
23        });
24        parent.setOnDragDone(event -> {
25            stage.setOpacity(1.0f);
26        });
27        parent.setOnMouseReleased(event -> {
28            stage.setOpacity(1.0f);
29        });
30    }