How to automatically resize windows in JavaFx for different resolutions?

I have the following problem:

I created a JavaFX window on the desktop with full hd, and I set the scene as follows:

 Scene scene = new Scene(root,1475,1015); 

When I run the application on a laptop with a resolution of 1360*760 , I don’t see the whole application, and I can’t change it.

How can I configure the application to automatically resize depending on the desktop / laptop and its resolution and size?

+6
source share
5 answers

I believe that you are looking for it

 GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); int width = gd.getDisplayMode().getWidth(); int height = gd.getDisplayMode().getHeight(); 

This will allow you to use the screen size of your device, and all you need to do to resize it makes the length / width of objects in your program proportional to the width and height of the screen.

+4
source

Mention that:

  1. You should use layout in JavaFX layouts (BorderPane, GridPane .... etc ...)

  2. This cannot be done automatically. You must program this.

  3. For example, usually you want to know the screen (width or height) without the taskbar (on Windows, Linux, Mac, Solaris). In this case, you are playing with getVisualBounds()...

Main theme


You ask about responsive design. Below is an example of what you want to do. Although this is not the best solution, by this I mean that it can be changed to improve performance (I also added some code to move the window if it is StageStyle.UNDECORATED Drag the window to see this):

enter image description here

 import javafx.application.Application; import javafx.scene.Cursor; import javafx.scene.Scene; import javafx.scene.input.MouseButton; import javafx.scene.layout.BorderPane; import javafx.scene.paint.Color; import javafx.stage.Screen; import javafx.stage.Stage; import javafx.stage.StageStyle; public class FX extends Application { int screenWidth = (int) Screen.getPrimary().getBounds().getWidth(); int screenHeight = (int) Screen.getPrimary().getBounds().getHeight(); Stage stage; Scene scene; int initialX; int initialY; @Override public void start(Stage s) throws Exception { // root BorderPane root = new BorderPane(); root.setStyle("-fx-background-color:rgb(186,153,122,0.7); -fx-background-radius:30;"); // Responsive Design int sceneWidth = 0; int sceneHeight = 0; if (screenWidth <= 800 && screenHeight <= 600) { sceneWidth = 600; sceneHeight = 350; } else if (screenWidth <= 1280 && screenHeight <= 768) { sceneWidth = 800; sceneHeight = 450; } else if (screenWidth <= 1920 && screenHeight <= 1080) { sceneWidth = 1000; sceneHeight = 650; } // Scene stage = new Stage(); stage.initStyle(StageStyle.TRANSPARENT); scene = new Scene(root, sceneWidth, sceneHeight, Color.TRANSPARENT); // Moving scene.setOnMousePressed(m -> { if (m.getButton() == MouseButton.PRIMARY) { scene.setCursor(Cursor.MOVE); initialX = (int) (stage.getX() - m.getScreenX()); initialY = (int) (stage.getY() - m.getScreenY()); } }); scene.setOnMouseDragged(m -> { if (m.getButton() == MouseButton.PRIMARY) { stage.setX(m.getScreenX() + initialX); stage.setY(m.getScreenY() + initialY); } }); scene.setOnMouseReleased(m -> { scene.setCursor(Cursor.DEFAULT); }); stage.setScene(scene); stage.show(); } /** * Main Method * * @param args */ public static void main(String[] args) { launch(args); } } 
+2
source

You can simply do this:

  primaryStage.setMaximized(true); 
+1
source

1-using JavaFX layouts (BorderPane, GridPane .... etc ...)

2-add some layout restrictions

You can use the scene builder to generate the FXML file and resize the screen to see what happens, see this tutorial for the layout

that is how you tell the program to draw your components based on constraints, so it will be an adaptive design, it will follow your constraints and automatically adjust the components.

0
source
 Scene scene = new Scene(root, Double.MAX_VALUE, Double.MAX_VALUE); 
0
source

Source: https://habr.com/ru/post/1258978/


All Articles