Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+19 votes

For the main menu of our screen, we have it set to a standardized size. However, other scenes we want to be much smaller in window size because it does not need to be the same size as the main menu window. Is there a simple fix to this? I tried setting the maxWidth and Height in fxml files however it didn't work.

asked in CSC305 Fall 2022 by (2k points)

3 Answers

+7 votes

scene objects have a "getWindow()" method which returns the Window object; Window objects have "setWidth(double width)" and "setHeight(double height)" methods that I believe are what you are looking for, example of usage below:

scene.getWindow().setWidth(3840);
scene.getWindow().setHeight(2160);
answered by (1 point)
+1

this worked although I had to make is App.scene.getWindow().setWidth(1500) and removed the "private" in "private static Scene scene" that was in app. Thank You!

+6 votes

The window in JavaFX is generally represented by a Stage object.

You might try calling:

 stage.sizeToScene()

after loading the new Scene on the Stage, to make the Stage size match the size for the scene you just loaded.

Haven't had a chance to test it yet... let me know if it works?

answered by (508 points)
+1

I tried putting this code in the onAction function in the controller where it sets the root of the app. I am not sure if it works since I don't see stage as a local variable and is only a parameter for the start function. I may try it out if the other way starts to bug.

+5 votes

Try this by manually changing the window screen for the different previews.

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

answered by (1 point)
...