Home:ALL Converter>Primary stage changes scene size

Primary stage changes scene size

Ask Time:2013-08-19T19:21:06         Author:Qri

Json Formatter

I have designed a simple scene with a few buttons in scene builder. Nothing special on that:

scenebuilder

I've put that on the primary stage and it looks ok to me:

resizable

But now, if i change the resizable attribute of the primary stage to false, the scene is growing on the right and on the bottom:

resizable

Why is the scene growing on the right and bottom? In my oppionion there is nothing special on it:

@Override
public void start(Stage stage) throws Exception {
    AnchorPane root = FXMLLoader.load(getClass().getResource("/fxml/start.fxml"));
    Scene scene = new Scene(root);
    stage.setResizable(false);
    stage.setTitle("Game");
    stage.setScene(scene);
    stage.centerOnScreen();
    stage.show();       

}

The FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>

<AnchorPane fx:id="rootPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="310.0" prefWidth="320.0" xmlns:fx="http://javafx.com/fxml" fx:controller="bla.bla.bla.Controller">
  <children>
    <Button fx:id="btnNewGame" layoutX="10.0" layoutY="10.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="300.0" text="New Game" />
    <Button fx:id="btnLoadGame" layoutX="10.0" layoutY="70.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="300.0" text="Load Game" />
    <Button fx:id="btnEditor" layoutX="10.0" layoutY="130.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="300.0" text="Editor" />
    <Button fx:id="btnProperties" layoutX="10.0" layoutY="190.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="300.0" text="Properties" />
    <Button fx:id="btnExit" layoutX="10.0" layoutY="250.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="300.0" text="Exit" />
  </children>
  <stylesheets>
    <URL value="@start.css" />
  </stylesheets>
</AnchorPane>

And the css:

#rootPane{
-fx-background-color: #333333;
}

.button{
-fx-background-color: #555555;
-fx-text-fill: silver;
-fx-background-radius: 0;
-fx-border-radius: 0;
-fx-border-color: #444444;   
}

Thanks in advance!

Author:Qri,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/18312563/primary-stage-changes-scene-size
zhujik :

My test program looks like this:\n\n@Override\n public void start(final Stage stage) throws Exception {\n\n StackPane root = FXMLLoader.load(getClass().getResource(\"stage.fxml\"));\n final Scene scene = new Scene(root);\n scene.widthProperty().addListener(new ChangeListener<Number>() {\n @Override\n public void changed(ObservableValue<? extends Number> observableValue, Number number, Number number2) {\n System.out.println(\"width: \"+number+\" -> \"+number2);\n }\n });\n boolean resizable = true;\n stage.setResizable(resizable);\n stage.setTitle(\"Stage Resizable \"+ resizable);\n stage.setScene(scene);\n stage.centerOnScreen();\n stage.show();\n }\n\n\nWhen the Stage#reziable flag is set to false, for example, setWidth is called twice. With your example fxml, my output is as follows:\n\nwidth: 0 -> 320\nwidth: 320 -> 330\n\n\nwhereas, when resizable is true, setWidth is only called once:\n\nwidth: 0 -> 320\n\n\nI cannot see where the 10 extra pixels are coming from, this has nothing to do with the interior of the scene (I switchted to an empty StackPane for my tests). I assumed that this is a bug in JavaFX, and a quick jira search revealed the following: https://javafx-jira.kenai.com/browse/RT-30647\n\nSo, if you want that to be fixed, I suggest you upvote that issue ;)",
2013-08-19T13:59:24
yy