Search code examples
javajavafxfxml

How to fix the window size in Scene Builder?


I have set the max/min width/height here

enter image description here

But it didn't seem to work,I still can change the size of my window when I preview it


Solution

  • The values you're setting are (probably) for the root Node. This won't prevent the Scene or Stage from growing or shrinking beyond the specified values. In fact, depending on the root Node, setting these values won't even stop said Node from growing or shrinking.

    From Javadoc of Scene:

    The application must specify the root Node for the scene graph by setting the root property. If a Group is used as the root, the contents of the scene graph will be clipped by the scene's width and height and changes to the scene's size (if user resizes the stage) will not alter the layout of the scene graph. If a resizable node (layout Region or Control) is set as the root, then the root's size will track the scene's size, causing the contents to be relayed out as necessary.

    If you want to constrain the size of a Stage you need to do it in code by setting the appropriate properties of the Stage: minWidth, minHeight, maxWidth, and maxHeight. Since you are setting minWidth = maxWidth and minHeight = maxHeight it would be better to use Stage.setResizable(false) (as mentioned in Bernhard's answer). This option also prevents the Stage from being maximized which setting the min/max size properties does not do. Though on my machine the properties do stop the Stage from growing when maximized but it causes the Stage to jump to the top-left corner.

    Since you don't have access to the preview Stage used by Scene Builder (as far as I know) I don't think you can constrain the size of the preview. One thing you may be able to deal with, however, is the fact the preview Stage opens to its previous size when closed. I'm not sure why this is default/unchangeable behavior but I'm guessing it's caused by using the same Stage each time. To resize the Stage to the preferred size you can go to:

    Preview -> Preview Size -> Preferred Size (Width x Height)
    

    Screenshot of menu item in Scene Builder

    This requires the preview to be showing otherwise the menu item is disabled.