Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving an undecorated stage in javafx 2

Tags:

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.

like image 853
Antonio J. Avatar asked Aug 02 '12 14:08

Antonio J.


People also ask

What is the difference between stage and scene JavaFX?

Stage , represents a window in a JavaFX desktop application. Inside a JavaFX Stage you can insert a JavaFX Scene which represents the content displayed inside a window - inside a Stage .


2 Answers

I created a sample of an animated clock in an undecorated window which you can drag around.

Relevant code from the sample is:

// allow the clock background to be used to drag the clock around. final Delta dragDelta = new Delta(); layout.setOnMousePressed(new EventHandler<MouseEvent>() {   @Override public void handle(MouseEvent mouseEvent) {     // record a delta distance for the drag and drop operation.     dragDelta.x = stage.getX() - mouseEvent.getScreenX();     dragDelta.y = stage.getY() - mouseEvent.getScreenY();   } }); layout.setOnMouseDragged(new EventHandler<MouseEvent>() {   @Override public void handle(MouseEvent mouseEvent) {     stage.setX(mouseEvent.getScreenX() + dragDelta.x);     stage.setY(mouseEvent.getScreenY() + dragDelta.y);   } });  ...  // records relative x and y co-ordinates. class Delta { double x, y; }  

Code looks pretty similar to yours, so not quite sure why your code is not working for you.

like image 126
jewelsea Avatar answered Oct 11 '22 04:10

jewelsea


I'm using this solution for dragging undecoraqted stages by dragging any contained node.

private void addDraggableNode(final Node node) {      node.setOnMousePressed(new EventHandler<MouseEvent>() {         @Override         public void handle(MouseEvent me) {             if (me.getButton() != MouseButton.MIDDLE) {                 initialX = me.getSceneX();                 initialY = me.getSceneY();             }         }     });      node.setOnMouseDragged(new EventHandler<MouseEvent>() {         @Override         public void handle(MouseEvent me) {             if (me.getButton() != MouseButton.MIDDLE) {                 node.getScene().getWindow().setX(me.getScreenX() - initialX);                 node.getScene().getWindow().setY(me.getScreenY() - initialY);             }         }     }); } 
like image 38
Zsolt Avatar answered Oct 11 '22 03:10

Zsolt