Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically resize windows in JavaFx for different resolutions?

I have the following problem:

I have created a JavaFX window on a desktop with full hd, and I set the scene like this:

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

When I run the application on a laptop with 1360*760 resolution, I can't see the whole application and I can't resize it.

How can I set my application to resize automatically in function of the desktop/laptop and it`s resolution and dimensions?

like image 294
Dina Bogdan Avatar asked Oct 29 '16 14:10

Dina Bogdan


People also ask

How do I fit to JavaFX screen?

If you want both of width and height of the nodes to fit to the screen, then you can : Use a gridpane. GridPane will resize the nodes of the screen according to screen size. Use a VBox inside an AncorPane and set the constraints to 0 , minSize to 0 , maxSize to Double.

How do I resize in Scene Builder?

Resize the Scene and the Scene Builder Window Resize the scene's width and height in the Content panel to get a larger working area. In the Inspector panel, select the Layout section. In the Size section, change the Pref Width property value to 800 and the Pref Height property value to 600.


2 Answers

I believe you are looking for this

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 is make the length/width of the objects within your program proportional to the width and height of the screen.

like image 176
Politic Revolutionnaire Avatar answered Nov 14 '22 22:11

Politic Revolutionnaire


Mention that:

  1. You have to use build in JavaFX layouts (BorderPane,GridPane.... etc...)

  2. It cannot be done automatically.You have to program it to do so.

  3. It is common for example that you want to know the screen(width or height) without the taskbar (in Windows,Linux,Mac,Solaris). In that case you play with getVisualBounds()...

Main theme


You are asking about Responsive Design.Below is an example of what you want to make.Although is not best solution,with this i mean it can be modified for better performance(I also have added some code to move the window if it is StageStyle.UNDECORATED Drag the Window to have 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);
    }

}
like image 45
GOXR3PLUS Avatar answered Nov 14 '22 21:11

GOXR3PLUS