2 Replies Latest reply on Nov 7, 2012 1:17 PM by jsmith

    Draggable window

    Martin Valenta
      Hi,
      I want to be able to drag my window which has no frame (StageStyle.TRANSPARENT) as well as you can drag a window using its frame if it has one. Is it possible to somehow hand this functionality over to some panel inside the window, e.g. the root panel?

      Look at the following example. Blue window can be dragged using its frame but the red one doesn't have any and therefore I can not drag it. I would like to be able to reposition it with DnD action on the root panel.
      import javafx.application.Application;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.layout.StackPane;
      import javafx.stage.Stage;
      import javafx.stage.StageStyle;
      
      public class StageDnD extends Application {
      
           public static void main(String[] args) {
                launch(args);
           }
           
           @Override
           public void start(Stage blueStage) {
                blueStage.setTitle("Hello World!");
                Button btn = new Button("hello");
                
                StackPane root = new StackPane();
                root.setStyle("-fx-background-color: blue;");
                root.getChildren().add(btn);
                blueStage.setScene(new Scene(root, 300, 250));
                blueStage.show();
                
                Stage redStage = new Stage(StageStyle.TRANSPARENT);
                redStage.setTitle("Hello World!");
                Button btn2 = new Button("hello");
                
                StackPane root2 = new StackPane();
                root2.setStyle("-fx-background-color: red;");
                root2.getChildren().add(btn2);
                redStage.setScene(new Scene(root2, 300, 250));
                redStage.show();
           }
      }
      Edited by: Martin Valenta on 7.11.2012 13:40
        • 1. Re: Draggable window
          shakir.gusaroff
          Hi. Add setMousePressed and setOnMouseDragged actions to your stage:
          import javafx.application.Application;
          import javafx.event.ActionEvent;
          import javafx.event.EventHandler;
          import javafx.scene.Scene;
          import javafx.scene.control.Button;
          import javafx.scene.layout.StackPane;
          import javafx.stage.Stage;
          import javafx.application.Application;
          import javafx.scene.Scene;
          import javafx.scene.control.Button;
          import javafx.scene.input.MouseEvent;
          import javafx.scene.layout.StackPane;
          import javafx.stage.Stage;
          import javafx.stage.StageStyle;
           
          public class StageDnd extends Application {
                double dragX = 0;
                double dragY = 0;
               public static void main(String[] args) {
                    launch(args);
               }
               
               @Override
               public void start(Stage blueStage) {
                    blueStage.setTitle("Hello World!");
                    Button btn = new Button("hello");
                    
                    StackPane root = new StackPane();
                    root.setStyle("-fx-background-color: blue;");
                    root.getChildren().add(btn);
                    blueStage.setScene(new Scene(root, 300, 250));
                    blueStage.show();
                    
               final     Stage redStage = new Stage(StageStyle.TRANSPARENT);
                    redStage.setTitle("Hello World!");
                    Button btn2 = new Button("hello");
                    
                    StackPane root2 = new StackPane();
                    root2.setStyle("-fx-background-color: red;");
                    root2.getChildren().add(btn2);
                          
                          
                          root2.setOnMousePressed(new EventHandler<MouseEvent>() {
                             public void handle (MouseEvent me) {
                                dragX = me.getScreenX() - redStage.getX();
                                dragY = me.getScreenY() - redStage.getY();
                                
                             }
                          
                          });
                          
                          
                          
                          root2.setOnMouseDragged(new EventHandler<MouseEvent>() {
                             public void handle (MouseEvent me) {
                                 
                                 redStage.setX(me.getScreenX() - dragX);
                                  redStage.setY(me.getScreenY() - dragY);
                               
                                
                             }
                          
                          });
                          
                          
                          
                    redStage.setScene(new Scene(root2, 300, 250));
                    redStage.show();
               }
          }
          You can also find it in James Weaver’s book Pro Javafx2
          1 person found this helpful
          • 2. Re: Draggable window
            jsmith
            There's a function to make a Stage draggable by an arbitrary node in this sample:
            https://gist.github.com/3388637