Dina Bogdan
Dina Bogdan

Reputation: 4698

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?

Upvotes: 6

Views: 36852

Answers (5)

user10937260
user10937260

Reputation:

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

Upvotes: 2

GOXR3PLUS
GOXR3PLUS

Reputation: 7255

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);
    }

}

Upvotes: 3

Mazen Embaby
Mazen Embaby

Reputation: 1361

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

2-add some layout constrains

you can use scene builder to generate your FXML file and resize your screen to see what's happening see this tutorial for layout

that's the way that you're telling the program to draw your components based on constrains so, in that way it will be responsive design it will follow your constrain and automatically adjust the components.

Upvotes: 0

nadhem
nadhem

Reputation: 188

You can just do that :

   primaryStage.setMaximized(true);

Upvotes: 2

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.

Upvotes: 6

Related Questions