Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8299968

Second call to Stage.setScene() create sizing issue with uiScale > 1.0

XMLWordPrintable

    • b09
    • generic
    • generic

        ADDITIONAL SYSTEM INFORMATION :
        Tested on Java 11 / 16 / 17 / 18 (Linux and Windows 10/11) and JavaFX 11, 16, 18 and 19

        A DESCRIPTION OF THE PROBLEM :
        Hello,

        I found out a problem and inconsistent behavior with setScene(...) method on stages.

        If the setScene(...) method is called on a stage when uiScale > 1, then the scene components are not layed out correctly (they are "out of bounds"). I think this is because the returned scene width/height are incorrect after a second setScene(...) call (with and height are higher than before, even if the stage size didn't change).

        If the stage is manually resized, the content is correctly layout out.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Have a uiScale > 1.0 on the system.

        1. Create a stage and set a scene inside
        2. Show this stage
        3. Create a new scene and set the scene to the stage
        4. The newly created scene is not displayed correctly

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        The newly created/set scene should be displayed correctly (its size should be the same than previous scene)
        ACTUAL -
        The newly created/set scene has wrong dimensions.

        The returned width/height are incorrect : they are computed as if the uiScale = 1.0 so the scene is not correctly displayed

        ---------- BEGIN SOURCE ----------
        import javafx.application.Application;
        import javafx.application.Platform;
        import javafx.geometry.Pos;
        import javafx.scene.Scene;
        import javafx.scene.control.Label;
        import javafx.scene.layout.VBox;
        import javafx.stage.Stage;

        public class ProblemSetSceneHighDPI extends Application {
            public static void main(String[] args) {
                launch(args);
            }

            @Override
            public void start(Stage primaryStage) throws Exception {
                primaryStage.setWidth(800);
                primaryStage.setHeight(600);
                primaryStage.setScene(createScene("This is centered"));
                primaryStage.show();
                new Thread(() -> {
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    Scene nScene = createScene("This is NOT centered");
                    Platform.runLater(() -> primaryStage.setScene(nScene));
                }).start();
            }

            private Scene createScene(String text) {
                VBox box = new VBox(new Label(text));
                box.setAlignment(Pos.CENTER);
                return new Scene(box);
            }
        }
        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        - User should resize the stage
        - Resize the stage with API

        These two methods are not great as they are directly impacting UI showed to user.

        FREQUENCY : always


              lkostyra Lukasz Kostyra
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

                Created:
                Updated:
                Resolved: