This discussion is archived
3 Replies Latest reply: Mar 10, 2013 5:57 PM by 995935 RSS

Stage should respect minimum size of its content

anouard Newbie
Currently Being Moderated
Hi,
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.
Thanks.

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 {
    
    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");

        StackPane root = new StackPane();
        
        // set minimum size
        root.setMinWidth(200);
        root.setMinHeight(200);
        
        root.getChildren().add(btn);
        
        Scene scene = new Scene(root, 300, 250);
        
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
  • 1. Re: Stage should respect minimum size of its content
    shakir.gusaroff Expert
    Currently Being Moderated
    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
    anouard Newbie
    Currently Being Moderated
    Hi,
    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.
    Thanks
  • 3. Re: Stage should respect minimum size of its content
    995935 Newbie
    Currently Being Moderated
    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.
    primaryStage.minHeightProperty().bind(primaryStage.heightProperty().subtract(scene.heightProperty()).add(root.minHeightProperty()));
    primaryStage.minWidthProperty().bind(primaryStage.widthProperty().subtract(scene.widthProperty()).add(root.minWidthProperty()));
    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

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points