6

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?

GOXR3PLUS
  • 6,877
  • 9
  • 44
  • 93
Dina Bogdan
  • 4,345
  • 5
  • 27
  • 56

5 Answers5

6

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.

3

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

}
Community
  • 1
  • 1
GOXR3PLUS
  • 6,877
  • 9
  • 44
  • 93
  • but how the others components inside the root will resize? – Dina Bogdan Oct 29 '16 at 15:45
  • @Dina Bogdan That's why you are using Layouts.It will be done automatically,but you have to define the logic.What i mean is ,say that at full hd 3 containers are displayed on the window.At 1200* resolution maybe or maybe not 3 containers can be displayed.So you will display only 2 or display them with different layout.You have to read a book about that.Can't be answered in one question...It's like you are making an application for computer which you want to be display on phone... – GOXR3PLUS Oct 29 '16 at 15:46
  • I understand what you are saying... I'm thinking thah I can use something like relative position or something like that. I want to run my application on all kind of computers and I have problems with GUI.... – Dina Bogdan Oct 29 '16 at 15:52
2

You can just do that :

   primaryStage.setMaximized(true);
nadhem
  • 178
  • 4
  • 20
2
Scene scene = new Scene(root, Double.MAX_VALUE, Double.MAX_VALUE);
  • For me this didnt allow the application to cover the usable screen area but it did launch the application window with a size proportionate to my screen size. Using `stage.SetMaximized(true)` as mentioned in another answer set it to the usable screen size although using both that and this answer makes for a clean looking application on minimize – Matt Oct 31 '22 at 17:08
0

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.

Mazen Embaby
  • 1,255
  • 11
  • 18