0

The program I'm making is a game with a game board. I load in the GUI from FXML, but I want to add the grid afterwards using JavaFX because it has to a be a variable size.

The GUI is a BorderPane, with some placeholder stuff snapped to the top and bottom, some filler Panes on the left and right, and a Pane in the center to hold a GridPane (the board) that gets populated with buttons. The GUI is designed to be resizeable and the grid will stay the size of it's contents but the Panes will stretch.

But for some reason, the grid doesn't appear. Either the buttons aren't getting added and the grid is collapsing, or the grid isn't getting added. Can someone please help? I really appreciate it.

(Assume I have all necessary imports.)

Edit: I have tried removing the initialization of the @FXML tagged variable and I get the same problem.

public class Menu {
    // Links to an id in the FXML
    @FXML
    public static Pane centerPane = new Pane();

    public static GridPane makeBoard(int size) {
        GridPane boardPane = new GridPane();
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                Button button = new Button();
                button.setMinHeight(80);
                button.setMinWidth(80);
                boardPane.add(button, i, j);
            }
        }
        return boardPane;
    }

    // This gets automatically called because it's called initialize and it's in
    the class set as Controller in the FXML
    public void initialize() {
        centerPane.getChildren().add(makeBoard(5));
        // Ideally something like this when the game is done
        // centerPane.getChildren().add(makeBoard(game.getBoard().getSize()));
    }
}
public class Main extends Application {

    private Game game;

    @Override
    public void start(Stage primaryStage) {
        try {
            Pane root = FXMLLoader.load(getClass().getResource("Layout.fxml"));
            Scene scene = new Scene(root);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

The FXML:

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.StackPane?>

<GridPane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Menu">
   <columnConstraints>
      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
   </columnConstraints>
   <rowConstraints>
      <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
   </rowConstraints>
   <children>
      <BorderPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity">
         <top>
            <StackPane maxWidth="1.7976931348623157E308" BorderPane.alignment="CENTER">
               <children>
                  <GridPane maxHeight="-Infinity" maxWidth="1.7976931348623157E308" prefHeight="150.0" prefWidth="600.0">
                    <columnConstraints>
                      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                    </columnConstraints>
                    <rowConstraints>
                      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                    </rowConstraints>
                     <children>
                        <Label text="Some text or a button or something">
                           <padding>
                              <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                           </padding>
                        </Label>
                        <Label text="Some text or a button or something" GridPane.columnIndex="1">
                           <padding>
                              <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                           </padding>
                        </Label>
                        <Label text="Some text or a button or something" GridPane.rowIndex="1">
                           <padding>
                              <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                           </padding>
                        </Label>
                        <Label text="Some text or a button or something" GridPane.columnIndex="1" GridPane.rowIndex="1">
                           <padding>
                              <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                           </padding>
                        </Label>
                        <Label text="Some text or a button or something" GridPane.rowIndex="2">
                           <padding>
                              <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                           </padding>
                        </Label>
                        <Label text="Some text or a button or something" GridPane.columnIndex="1" GridPane.rowIndex="2">
                           <padding>
                              <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                           </padding>
                        </Label>
                     </children>
                  </GridPane>
               </children>
            </StackPane>
         </top>
         <bottom>
            <StackPane maxWidth="1.7976931348623157E308" BorderPane.alignment="CENTER">
               <children>
                  <GridPane prefHeight="50.0" prefWidth="600.0">
                    <columnConstraints>
                      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                    </columnConstraints>
                    <rowConstraints>
                      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                    </rowConstraints>
                     <children>
                        <Label text="Some text or a button or something">
                           <padding>
                              <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                           </padding>
                        </Label>
                        <Label text="Some text or a button or something">
                           <padding>
                              <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                           </padding>
                        </Label>
                        <Label text="Some text or a button or something" GridPane.columnIndex="1">
                           <padding>
                              <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                           </padding>
                        </Label>
                     </children>
                  </GridPane>
               </children>
            </StackPane>
         </bottom>
         <center>
            <Pane fx:id="centerPane" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
         </center>
      </BorderPane>
   </children>
</GridPane>
Bob
  • 61
  • 1
  • 6
  • 2
    Never initialize fields annotated `@FXML`. – James_D Dec 03 '20 at 14:49
  • I've tried that (edited post to tell others). – Bob Dec 03 '20 at 15:49
  • The underlying problem is explained in the linked question: you made `centerPane` `static`. – James_D Dec 03 '20 at 16:04
  • 1
    If you initialize `centerPane` it is no longer the `Pane` defined in the FXML file - it's the `Pane` you created in code. So it's not actually displayed in the border pane. Hence when you add elements to it, you won't see anything. – James_D Dec 03 '20 at 16:06
  • @James_D seeing the instantiated vs. fxml annotated field error on a daily basis - isn't there a plugin or a rule for the typical qa tools that might find those? Can't be too difficult to include it .. not a tool person myself, so just musing :) – kleopatra Dec 03 '20 at 16:10
  • Thank you James D you have saved me. I am v grateful. – Bob Dec 03 '20 at 16:34

0 Answers0