/* when mouse button is pressed, save the initial position of screen. */ root.setOnMousePressed(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent me) { Window primaryStage = root.getScene().getWindow(); anchor.set(new Point2D(me.getScreenX() - primaryStage.getX(), me.getScreenY() - primaryStage.getY())); widthStage.set(primaryStage.getWidth()); heightStage.set(primaryStage.getHeight()); } });
/* * when mouse button is released, clear the initial position of screen. This action is very important, because it can be a judge to avoid * mouse interaction with other nodes as ChoiceBox. */ root.setOnMouseReleased(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent me) { anchor.set(null); } });
/* * when screen is dragged, translate it accordingly. Note that the transparent pixels part contained in the root will not react to the drag. * But translucent color is able to. */ root.setOnMouseDragged(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent me) { if (anchor.get() != null) { /* The drag event on the root really takes place. */ drag(me, root); } } }); }
/** * Drag the primary stage. * * @param me * - the mouse event * @param root * - the root node of stage */ private void drag(MouseEvent me, Node root) { Window primaryStage = root.getScene().getWindow(); if (root.getCursor() == Cursor.H_RESIZE) { setWidth(me, primaryStage); } else if (root.getCursor() == Cursor.V_RESIZE) { setHeight(me, primaryStage); } else if (root.getCursor() == Cursor.SE_RESIZE) { setWidth(me, primaryStage); setHeight(me, primaryStage); } else { /* moving the stage */ moveStage(me, primaryStage); } }
/** * Set the stage width. * * @param me * - the mouse event * @param primaryStage * - the primary stage */ private void setWidth(MouseEvent me, Window primaryStage) { double stageMinWidth = 600; primaryStage.setWidth(Math.max(stageMinWidth, widthStage.get() + (me.getScreenX() - (anchor.get().getX() + primaryStage.getX())))); }
/** * Set the stage height. * * @param me * - the mouse event * @param primaryStage * - the primary stage */ private void setHeight(MouseEvent me, Window primaryStage) { double stageMinHeight = 400; primaryStage.setHeight(Math.max(stageMinHeight, heightStage.get() + (me.getScreenY() - (anchor.get().getY() + primaryStage.getY())))); }