Moving an undecorated stage in javafx 2

Viewed 27073

I've been trying to move an undecorated stage around the screen, by using the following mouse listeners:

  • onPressed
  • onReleased
  • onDragged

These events are from a rectangle. My idea is to move the undecorated window clicking on the rectangle and dragging all the window.

@FXML
protected void onRectanglePressed(MouseEvent event) {
    X = primaryStage.getX() - event.getScreenX();
    Y = primaryStage.getY() - event.getScreenY();
}

@FXML
protected void onRectangleReleased(MouseEvent event) {
    primaryStage.setX(event.getScreenX());
    primaryStage.setY(event.getScreenY());
}

@FXML
protected void onRectangleDragged(MouseEvent event) {
    primaryStage.setX(event.getScreenX() + X);
    primaryStage.setY(event.getScreenY() + Y);
}

All that I've got with these events is when I press the rectangle and start to drag the window, it moves a little bit. But, when I release the button, the window is moved to where the rectangle is.

Thanks in advance.

5 Answers
Related