[英]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. 我在
GridPane
中创建了一个9x9 GridPane
,并且我想向每个单元格添加单独的TextFields
。 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; 我不想在FXML / SceneBuilder中添加
TextFields
。 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
. 我想跟踪它们在数组中的位置,以便可以访问它们的各个值,因此我想一次在Controller中创建一个值,然后将它们添加到数组以及
GridPane
每个单元格中。
Here is the part of my controller that attempts to add TextFields
(I tried creating them before adding them to the array): 这是我的控制器尝试添加
TextFields
(我尝试在将它们添加到数组之前创建它们):
@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 编辑:我看了
StackTrace
并注意到我得到一个空指针
gridPane.add(tempTextField,i,j);
FXML File : FXML文件 :
<?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()
逻辑移至其随附的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.