3 Replies Latest reply on Mar 11, 2013 12:57 AM by 995935

    Stage should respect minimum size of its content

      I wonder if there was a standardized approach to make the Stage inherits its minimum size from its content.
      I tried to bind the Stage's mimimumWidthProperty to StackPane equivalent but without any success.

      The only way I found was to 'force' the size by explicitly call stage.setMinWidth(), but this not really dynamic.
      For information, the preferred size works fine and is well managed by the Stage.

      If the following example, the Stage does not respect the minimum size set on the StackPane:
      import javafx.application.Application;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.layout.StackPane;
      import javafx.stage.Stage;
      public class TestMinSize extends Application {
          public void start(Stage primaryStage) {
              Button btn = new Button();
              btn.setText("Say 'Hello World'");
              StackPane root = new StackPane();
              // set minimum size
              Scene scene = new Scene(root, 300, 250);
              primaryStage.setTitle("Hello World!");
        • 1. Re: Stage should respect minimum size of its content
          Hi. Binding the stackpane size to the stage size doesn’t make sense. The stage size will be greater than the size of the stackpane.
          The stage size is defined based on decorations of the windows. Decorations includes title bar and borders.
          If you add the following lines to the end of your program:
               System.out.println( "Stage width is:  "  + primaryStage.getWidth());
               System.out.println( "Stage height is:  "  + primaryStage.getHeight());
               System.out.println( "Scene width is:  "  + scene.getWidth());
               System.out.println( "Scene height is:  "  + scene.getHeight());
          you will get the following result (on Windows 7):
          Stage width is:  316.0
          Stage height is:  288.0
          Scene width is:  300.0
          Scene height is:  250.0
          The stage width and height are greater than the scene width and height.
          It is better to set the scene width and height rather setting the width and height of the stage.
          • 2. Re: Stage should respect minimum size of its content
            Thanks for your advice. Unfortunately, I only talked about the "*minimum* *size*" that the Stage does not reflect.
            Btw, in my case, I'm controlling everything since the stage is set as Undecorated.
            • 3. Re: Stage should respect minimum size of its content
              The only thing that springs to mind is binding the properties to substract the decoration size of the stage.
              But this wouldn't take into account if a child node of your root node has a minWidth/Height larger than the root's.
              And generally this only works if root has a minSizeProperty, but Scene.getRoot() is a Parent.
              Or slightly more general:
              public static void respectMinimumSize(Stage stage) {
                   Scene scene = stage.getScene();
                   if (scene.getRoot() instanceof Region) {
                        Region root = (Region) scene.getRoot();
                        stage.minHeightProperty().bind(Bindings.max(0, stage.heightProperty().subtract(scene.heightProperty()).add(root.minHeightProperty())));
                        stage.minWidthProperty().bind(Bindings.max(0, stage.widthProperty().subtract(scene.widthProperty()).add(root.minWidthProperty())));
              Edited by: Dejay on Mar 10, 2013 5:56 PM

              Edited by: Dejay on Mar 10, 2013 5:57 PM