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.
I'm using this solution for dragging undecoraqted stages by dragging any contained node.
Based on jewelsea's reply i made two lamba expressions to set the MouseListeners directly on the scene in my start() method. Works fine :)
I created a sample of an animated clock in an undecorated window which you can drag around.
Relevant code from the sample is:
Code looks pretty similar to yours, so not quite sure why your code is not working for you.
My guess is that your:
is passing the very same coordinates from your
This happens because as stated in the documentation the getX() method from the MouseEvent class returns
then, when you release your mouse it actually puts your rectangle in the place it was when you first mouse pressed.
BTW, I use a StackPane with a Rectangle inside and then apply the following code:
Besides that, I use the Middle Mouse Button to center my Window on the screen. I hoped it helped. Cheers!