在FXML中,我有根BorderPane
,在其中心是另一个BorderPane
,在第二个Borderpane
的中心,我有GridPane。我想根据阵列的大小添加TextField
。我不知道如何访问该GridPane。在FXML中,我设置了fx:id,但是在我的控制器中它不可见。(@FXML GridPane nameInFxml
)
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="466.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:id="rootBorderPane">
<left>
<TableView fx:id="repairsTable" prefHeight="372.0" prefWidth="151.0" BorderPane.alignment="CENTER">
<columns>
<TableColumn fx:id="data" prefWidth="55.0" text="Data">
<cellValueFactory>
<PropertyValueFactory property="data" />
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="mechanik" prefWidth="41.0" text="Mechanik">
<cellValueFactory>
<PropertyValueFactory property="mechanik" />
</cellValueFactory>
</TableColumn>
</columns></TableView>
</left>
<center>
<BorderPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" fx:id="borderPane">
<center>
<GridPane alignment="CENTER" hgap="5.0" prefHeight="300.0" prefWidth="448.0" BorderPane.alignment="CENTER" fx:id="details" >
<columnConstraints>
<ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" maxWidth="220.0" minWidth="10.0" prefWidth="71.0" />
<ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" maxWidth="346.0" minWidth="10.0" prefWidth="244.0" />
<ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" maxWidth="346.0" minWidth="10.0" prefWidth="66.0" />
<ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" maxWidth="346.0" minWidth="10.0" prefWidth="80.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="95.0" minHeight="0.0" prefHeight="25.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="200.0" minHeight="0.0" prefHeight="23.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="290.0" minHeight="10.0" prefHeight="290.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label fx:id="coByloRobioneLabel" text="Co było robione" GridPane.columnIndex="0" />
<Label fx:id="nettoLabel" text="Netto" GridPane.columnIndex="01" />
<Label fx:id="bruttoLabel" text="Brutto" GridPane.columnIndex="2" />
</children>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</GridPane>
</center>
<top>
<GridPane prefHeight="51.0" prefWidth="449.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>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label fx:id="mechanikLabel" text="Mechanik " GridPane.columnIndex="0" />
<Label text="Data" GridPane.columnIndex="2" />
<Label text="Nr Rej." GridPane.columnIndex="4" />
<TextField fx:id="mechanikTextField" GridPane.columnIndex="1" />
<TextField fx:id="dataTextField" GridPane.columnIndex="3" />
<TextField fx:id="nrRejTextField" GridPane.columnIndex="5" />
</children>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</GridPane>
</top>
</BorderPane>
</center>
<bottom>
<HBox alignment="CENTER" prefHeight="44.0" prefWidth="600.0" spacing="20" BorderPane.alignment="CENTER">
<children>
<TextField fx:id="searchTextField" />
<Button fx:id="nowyButton" mnemonicParsing="false" text="Nowy" />
<Button fx:id="zapiszButton" mnemonicParsing="false" text="Zapisz" />
<Button fx:id="usunButton" mnemonicParsing="false" text="Usuń" />
<Button fx:id="newRecord" mnemonicParsing="false" text="Nowa pozycja w naprawie" />
</children>
</HBox>
</bottom>
</BorderPane>
我想使用fx:id =“ details”访问该GridPane。
控制器:
public class RepairsController {
@FXML
TextField mechanikTextField;
@FXML
TextField dataTextField;
@FXML
TextField nrRejTextField;
@FXML
Button nowyButton;
@FXML
Button usunButton;
@FXML
Button zapiszButton;
@FXML
TextField searchTextField;
@FXML
TableView<Repair> repairsTable;
@FXML
GridPane details;
private ObservableList<Repair> repairs;
private Car car;
public RepairsController() {
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public void initialize(){
repairs = DataSource.getInstance().queryAllRepairs(car.getNrRej());
filter(repairs);
onChangeListener();
repairsTable.getSelectionModel().selectFirst();
nowyButtonPressed();
usunButtonPressed();
}
@FXML
public void filter(ObservableList<Repair> list) {
FilteredList<Repair> filteredData = new FilteredList<>(list, p -> true);
searchTextField.textProperty().addListener((observable, oldValue, newValue) -> {
filteredData.setPredicate(repair-> {
if (newValue == null || newValue.isEmpty()) {
return true;
}
String loweCaseFilter = newValue.toLowerCase();
if (repair.getData().toLowerCase().contains(loweCaseFilter)) {
return true;
}
if((repair.getMechanik().toLowerCase().contains(loweCaseFilter))){
return true;
}
return false;
});
});
SortedList<Repair> sortedData = new SortedList<>(filteredData);
sortedData.comparatorProperty().bind(repairsTable.comparatorProperty());
repairsTable.setItems(sortedData);
}
private void onChangeListener(){
repairsTable.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Repair>() {
@Override
public void changed(ObservableValue<? extends Repair> observableValue, Repair repair, Repair t1) {
if(t1 != null) {
mechanikTextField.setText(repairsTable.getSelectionModel().getSelectedItem().getMechanik());
dataTextField.setText(repairsTable.getSelectionModel().getSelectedItem().getData());
}
TextField[] coTextField = new TextField[30];
if(repairsTable.getSelectionModel().getSelectedItem().getRepairDetailsList() !=null) {
for (int i = 0; i < repairsTable.getSelectionModel().getSelectedItem().getRepairDetailsList().size(); i++) {
//at this point I get the exception
details.add(coTextField[i], 2, 2);
}
}
}
});
}
public void nowyButtonPressed(){
nowyButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
Repair repair = new Repair();
repairs.add(repair);
}
});
}
public void usunButtonPressed(){
usunButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
repairs.remove(repairsTable.getSelectionModel().getSelectedItem());
}
});
}
}
答案 0 :(得分:2)
您将收到对此行的空指针
TextField[] coTextField = new TextField[30];
此数组中的所有Textfields都没有初始化,您刚刚创建了一个大小为30的空数组,可以容纳TextFields
如果您将此代码放在该行之后{TextField[] coTextField = new TextField[30];
),我认为您的NPE将消失
for (int i = 0; i < coTextField.length; i++)
coTextField[i] = new TextField();
此代码将为数组中的每个索引创建一个new Textfield
如果您对此帖子有任何疑问,请告诉我