Qri
Qri

Reputation: 237

Primary stage changes scene size

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!

Upvotes: 4

Views: 2467

Answers (2)

Flo C
Flo C

Reputation: 421

I'm not familiar with FXML but did you try stage.sizeToScene(); in your start method?

Edit: i'm sorry it doesn't work, and reading Sebastian answers makes it obvious.

Upvotes: 0

zhujik
zhujik

Reputation: 6574

My test program looks like this:

@Override
  public void start(final Stage stage) throws Exception {

    StackPane root = FXMLLoader.load(getClass().getResource("stage.fxml"));
    final Scene scene = new Scene(root);
    scene.widthProperty().addListener(new ChangeListener<Number>() {
      @Override
      public void changed(ObservableValue<? extends Number> observableValue, Number number, Number number2) {
        System.out.println("width: "+number+" -> "+number2);
      }
    });
    boolean resizable = true;
    stage.setResizable(resizable);
    stage.setTitle("Stage Resizable "+ resizable);
    stage.setScene(scene);
    stage.centerOnScreen();
    stage.show();
  }

When the Stage#reziable flag is set to false, for example, setWidth is called twice. With your example fxml, my output is as follows:

width: 0 -> 320
width: 320 -> 330

whereas, when resizable is true, setWidth is only called once:

width: 0 -> 320

I 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

So, if you want that to be fixed, I suggest you upvote that issue ;)

Upvotes: 2

Related Questions