How to make a non-docked window movable / draggable in JavaFX?

I need to create an application in which the minimize and enlarge button is disabled.

I used "StageStyle.UNDECORATED" with which the application will no longer move or drag, so I'm looking for any other alternative for my application.

Who has a solution for this?

+13
javafx-2 minimize maximize
source share
2 answers

To ensure that the window is not decorated, but is still movable / draggable, you must handle the corresponding MouseEvent on any node of your choice.

Example:

 import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.input.MouseEvent; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; import javafx.stage.StageStyle; public class SimpleWindowApplication extends Application { private double xOffset = 0; private double yOffset = 0; public static void main(String[] args) { launch(args); } @Override public void start(final Stage primaryStage) { primaryStage.initStyle(StageStyle.UNDECORATED); BorderPane root = new BorderPane(); root.setOnMousePressed(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { xOffset = event.getSceneX(); yOffset = event.getSceneY(); } }); root.setOnMouseDragged(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { primaryStage.setX(event.getScreenX() - xOffset); primaryStage.setY(event.getScreenY() - yOffset); } }); Scene scene = new Scene(root, 800, 600); primaryStage.setScene(scene); primaryStage.show(); } } 

Learn more from the very valuable examples provided on the Oracle JavaFX download page under: JavaFX Demos and Samples

+20
source share

The only purpose of this class is to allow dragging and dropping an unfinished window. It also has responsibilities to ensure that the TaskBar remains visible with FullScreen and ensures that an undeclared window is not displayed. Finally, it provides a bug fix for the "css resource not found" error. Just paste the code below in the main class into the overridden start () method when the “READY” stage is displayed or after it.

 WindowStyle.allowDrag(root, stage); WindowStyle.stageDimension(stage.getWidth(), stage.getHeight()); 

NOTE. Paste the above value if “Stage” should be set to “READY” for it to appear or after it. For a full screen window, use:

 WindowStyle.fullScreen(Stage stage); 

To resize previous use:

 WindowStyle.restoreScreen(Stage stage); 

To add custom stylesheets to your scene, simply paste the start () override method after defining your scene under the code in the main class.

 scene.getStylesheets().add(WindowStyle.addStyleSheet(String css)); 

The css name that will be used for styling can be in the form: main.css or styles/main.css

 import javafx.geometry.Rectangle2D; import javafx.scene.Parent; import javafx.scene.input.MouseEvent; import javafx.stage.Screen; import javafx.stage.Stage; /** * @author: BENJAH */ public class WindowStyle { private static final Rectangle2D SCREEN_BOUNDS= Screen.getPrimary() .getVisualBounds(); private static double[] pref_WH, offset_XY; private static String styleSheet; private WindowStyle(String css) { styleSheet= getClass().getResource(css).toString(); } protected static void allowDrag(Parent root, Stage stage) { root.setOnMousePressed((MouseEvent p) -> { offset_XY= new double[]{p.getSceneX(), p.getSceneY()}; }); root.setOnMouseDragged((MouseEvent d) -> { //Ensures the stage is not dragged past the taskbar if (d.getScreenY()<(SCREEN_BOUNDS.getMaxY()-20)) stage.setY(d.getScreenY() - offset_XY[1]); stage.setX(d.getScreenX() - offset_XY[0]); }); root.setOnMouseReleased((MouseEvent r)-> { //Ensures the stage is not dragged past top of screen if (stage.getY()<0.0) stage.setY(0.0); }); } //Sets the default stage prefered width and height. protected static void stageDimension(Double width, Double height) { pref_WH= new double[]{width, height}; } protected static void fullScreen(Stage stage) { stage.setX(SCREEN_BOUNDS.getMinX()); stage.setY(SCREEN_BOUNDS.getMinY()); stage.setWidth(SCREEN_BOUNDS.getWidth()); stage.setHeight(SCREEN_BOUNDS.getHeight()); } protected static void restoreScreen(Stage stage) { stage.setX((SCREEN_BOUNDS.getMaxX() - pref_WH[0])/2); stage.setY((SCREEN_BOUNDS.getMaxY() - pref_WH[1])/2); stage.setWidth(pref_WH[0]); stage.setHeight(pref_WH[1]); } protected static String addStyleSheet(String css) { new WindowStyle(css); return styleSheet; } } 
0
source share

All Articles