user9507446
user9507446

Reputation: 391

JavaFX isn't printing a grid on a BorderPane

I'm trying to write a simple sudoku interface in JavaFX using text fields, however when I try to generate the grid, it simply does not show up. I assume that there may be a problem with the fxml file, but I don't know whether it's even necessary.

I tried to use Gluon SceneBuilder, but I intend to avoid generating it manually, inasmuch as it generates excessive amount of code.

My game controlling component is as follows:

public class GameController implements Initializable {

    public Pane gamePane;

    @FXML
    private GridPane gridPane;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        gridPane = new GridPane();
        for (int i = 0; i < SudokuBoard.ROW_NUM; i++) {
            for (int j = 0; j < SudokuBoard.ROW_NUM; j++) {
                TextField field = new TextField(String.valueOf(0));
                field.setFont(Font.font(24));
                gridPane.add(field, i, j);
            }
        }

        gamePane.getChildren().add(gridPane);

    }
}

and my fxml file:

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

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>


<?import javafx.scene.layout.GridPane?>
<BorderPane fx:id="gamePane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <top>
      <Label fx:id="gameLabel" text="Sudoku Game" BorderPane.alignment="CENTER" />
   </top>
   <center>
      <GridPane/>
   </center>
</BorderPane>

I think manipulating the code this way could produce the expected result, but it does not. Could it be that the fxml file hinders the automatic generation of grid pane text fields?

Upvotes: 1

Views: 179

Answers (1)

Matt
Matt

Reputation: 3187

The problems I saw were all missing a few things but after fixing it ran well for me first off you had no controller connected to the fxml you may actually not need this depending on how you load your fxml(Which I can't see)

I just had to add this section to the boarderpane part

fx:controller="FXMLFolder.Controller"

or depending on your project structure

fx:controller="Controller"

like so

<BorderPane fx:id="gamePane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" 
        prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1"  xmlns:fx="http://javafx.com/fxml/1" 
        fx:controller="FXMLTests.Controller">

Next you call a gridpane that looks like its attached to the fxml because the annotation but its not as you don't have the fx:id in the fxml like so change this

<GridPane/>

to

<GridPane fx:id="gridPane"/>

try to stay consistent when declaring your variable so you don't confuse others reading your code so match up

public Pane gamePane;
@FXML
private GridPane gridPane;

to

public Pane gamePane;
public GridPane gridPane;
public Label gameLabel;//I added this because you have an `fx:id` in your fxml

Upvotes: 1

Related Questions