I am writing a mini-game on JavaFX, in order to familiarize myself with this technology, and I encountered an unusual problem. When you add a playing field ( GridPane ) to the root panel, which is a StackPane , it (the playing field) is displayed on the screen as expected, but the getWidth() and getHeight() methods return 0.0 . Because of this, it is impossible to add game objects to the field and generally interact with it. I add the field itself
root.getChildren().add(fightScreen); Where root is StackPane , fightScreen is GridPane .
In the game there are other GridPane , with them there is no problem. The problem is only with this panel.
I created everything with the help of SceneBuilder, but just in case I will give the code for this panel
<GridPane focusTraversable="true" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="700.0" prefWidth="1000.0" styleClass="grid-pane" stylesheets="@csss/FightScreen.css" vgap="10.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.view.FightScreenController"> <columnConstraints> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="555.0" /> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="111.0" /> </columnConstraints> <rowConstraints> <RowConstraints maxHeight="229.0" minHeight="0.0" prefHeight="35.0" vgrow="SOMETIMES" /> <RowConstraints maxHeight="229.0" minHeight="0.0" prefHeight="60.0" vgrow="SOMETIMES" /> <RowConstraints maxHeight="483.0" minHeight="10.0" prefHeight="344.0" vgrow="SOMETIMES" /> <RowConstraints maxHeight="304.0" minHeight="10.0" prefHeight="190.0" vgrow="SOMETIMES" /> <RowConstraints maxHeight="303.0" minHeight="10.0" prefHeight="59.0" vgrow="SOMETIMES" /> </rowConstraints> <children> <Label text="Hero" textFill="#ba1818"> <padding> <Insets left="20.0" top="10.0" /> </padding> <font> <Font name="Comic Sans MS" size="36.0" /> </font> </Label> <Label text="Enemy" GridPane.columnIndex="2" GridPane.halignment="RIGHT"> <padding> <Insets right="20.0" top="10.0" /> </padding> <font> <Font name="Britannic Bold" size="36.0" /> </font> </Label> <HBox fx:id="hero" prefHeight="100.0" prefWidth="200.0" spacing="5.0" GridPane.rowIndex="1" /> <HBox fx:id="enemy" prefHeight="100.0" prefWidth="200.0" spacing="5.0" GridPane.columnIndex="2" GridPane.rowIndex="1" /> <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="190.0" prefWidth="373.0" styleClass="anchor-pane-2" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="3" GridPane.valignment="CENTER" /> <AnchorPane prefHeight="200.0" prefWidth="200.0" styleClass="anchor-pane-1" GridPane.columnIndex="1" GridPane.rowIndex="2" /> </children> </GridPane> I would be grateful if you tell me how to solve this problem.