Creating a scene using both FXML and the Controller in JavaFX

大城市里の小女人 提交于 2019-12-13 08:17:23

问题


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>

回答1:


Try implementing the javafx.fxml.Initializable interface in your controller and move the setGrid() logic to the method that comes with it.



来源:https://stackoverflow.com/questions/46545512/creating-a-scene-using-both-fxml-and-the-controller-in-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!