How to drag an undecorated window (stage) of JavaFX
Just change your setOnMousePressed method to this:
bp.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
xOffset = primaryStage.getX() - event.getScreenX();
yOffset = primaryStage.getY() - event.getScreenY();
}
});
and your setOnMouseDragged to this:
bp.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
primaryStage.setX(event.getScreenX() + xOffset);
primaryStage.setY(event.getScreenY() + yOffset);
}
});
The @Eeliyaanswer working well. However, code will shorter if using the below code because we don't need to declare variables.
We need to setOnMouseDragged
again and again whenever setOnMouseDragged
fired but I think it's not causing the problem.
bp.setOnMousePressed(pressEvent -> {
bp.setOnMouseDragged(dragEvent -> {
primaryStage.setX(dragEvent.getScreenX() - pressEvent.getSceneX());
primaryStage.setY(dragEvent.getScreenY() - pressEvent.getSceneY());
});
});
Hope it helps