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

Width/height of window is not set after calling sizeToScene

XMLWordPrintable

    • b05
    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      OS: RedHat Linux 9.1, Gnome 40.10
      JRE: 17 (2021-09-14)
      JFX: 20.0.1

      A DESCRIPTION OF THE PROBLEM :
      If sizeToScene is called on a hidden stage and later setWidth/setHeight are called while the stage is still hidden, the values set are ignored when the stage is finally shown. The width and height correspond to the size of the scene.
      The culprit is most likely this code (in Window#showing):
      ...
                  if (sizeToScene) {
                      if (newVisible) {
                          // Now that the visibleChanged has completed, the insets of the window
                          // might have changed (e.g. due to setResizable(false)). Reapply the
                          // sizeToScene() request if needed to account for the new insets.
                          sizeToScene();
                      }

                      // Reset the flag unconditionally upon visibility changes
                      sizeToScene = false;
                  }
      ...

      I think setting setWidth/Height should unset the sizeToScene flag (given that it means that the user no longer wants the window to be sized to the scene).
      I can provide a PR if this solution seems correct.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See the description or example code.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The window should have size 1000x1000.
      ACTUAL -
      The window has size 27x45.

      ---------- BEGIN SOURCE ----------
      import javafx.application.Application;
      import javafx.scene.Scene;
      import javafx.scene.control.Label;
      import javafx.stage.Stage;

      public class StageSizeTest extends Application {
          @Override
          public void start(final Stage primaryStage) {
              final var scene = new Scene(new Label("test"));
              primaryStage.setScene(scene);
              primaryStage.sizeToScene();
              primaryStage.setWidth(1000);
              primaryStage.setHeight(1000);
              primaryStage.show();
              System.out.println(primaryStage.getWidth());
              System.out.println(primaryStage.getHeight());
          }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


            arapte Ambarish Rapte
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: