简体   繁体   中英

Creating a scene using both FXML and the Controller in JavaFX

I've created a 9x9 GridPane in SceneBuilder, and I want to add individual TextFields to each cell. I'm certain there are other methods for creating a large table like this, but I'm not looking for a different way to do this (this is part of my learning experience). I don't want to add TextFields in FXML/SceneBuilder; I want to keep track of them in an array so I can access their individual values, so I want to create them one at a time in the Controller and then add them to the array as well as to each cell of the GridPane .

Here is the part of my controller that attempts to add TextFields (I tried creating them before adding them to the array):

@FXML
private GridPane gridPane;
private TextField myTextField[][] = new TextField[9][9];
.
.
.
@Override
public void initialize(URL url, ResourceBundle rb) {
  for (int i = 0; i < 9; ++i){
        for (int j = 0; j < 9; ++j){

            TextField tempTextField = new TextField();
            Font myFont = new Font("System",38);
            tempTextField.setFont(myFont);
            tempTextField.setText(i + ", " + j);
            tempTextField.setPrefSize(70, 70);
            myTextField[i][j] = tempTextField;
            gridPane.add(tempTextField,i,j);
            System.out.println("TextField " +i+j+" Created!");
        }
    } 
}

I don't get an error before runtime and the scene is not updated.

EDIT: I have looked at the StackTrace and noticed that I'm getting a Null Pointer at

gridPane.add(tempTextField,i,j);

FXML File :

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

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

<AnchorPane id="AnchorPane" prefHeight="654.0" prefWidth="747.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="sudokusolver.FXMLDocumentController">
    <children>
      <BorderPane prefHeight="654.0" prefWidth="747.0">
         <center>
            <GridPane fx:id="gridPane" gridLinesVisible="true" prefHeight="198.0" prefWidth="200.0" BorderPane.alignment="CENTER">
               <columnConstraints>
              <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <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 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 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>
            </GridPane>
         </center>
         <bottom>
            <HBox alignment="CENTER" prefHeight="40.0" prefWidth="747.0" BorderPane.alignment="CENTER">
               <children>
                  <FlowPane prefHeight="200.0" prefWidth="200.0">
                     <children>
                        <Button fx:id="loadButton" alignment="CENTER" contentDisplay="CENTER" minHeight="-Infinity" minWidth="-Infinity" onAction="#setGrid" prefHeight="35.0" prefWidth="100.0" text="Load Board" />
                          <Button fx:id="solveButton" alignment="CENTER" contentDisplay="CENTER" minHeight="-Infinity" minWidth="-Infinity" onAction="#sudokuSolve" prefHeight="35.0" prefWidth="100.0" text="Solve" />
                     </children>
                  </FlowPane>
               </children>
            </HBox>
         </bottom>
      </BorderPane>
    </children>
</AnchorPane>

尝试在控制器中实现javafx.fxml.Initializable接口,并将setGrid()逻辑移至其随附的方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM