Я хотел бы сделать что-то вроде это , но динамически.
У меня есть сцена "Configuration.fxml" и "ControllerConfiguration.java", и я хотел бы добавить в его GridPane "gp" "ESP_button.fxml" и прочитать текстовые поля после их изменения. (Фотографии ниже)
Итак, мне удалось добавить много строк с одним и тем же ESP_button.fxml, но я не могу получить доступ к их текстовым полям, потому что я получаю нулевое исключение PointerException.
Я также пытался создать загрузчик для ESP_button и Controller для получения TextField от контроллера ESP_button, но я все еще получаю исключение NullPointerException.
Это мой код:
ConfigurationController.java
@FXML private GridPane gp;
ObservableList<TextField> macTextField = FXCollections.observableArrayList();
ObservableList<TextField> xTextField = FXCollections.observableArrayList();
ObservableList<TextField> yTextField = FXCollections.observableArrayList();
private int nEsp = 0;
GridPane espButton ;
private void addEspButton(int x, int y) throws IOException {
GridPane eb = (GridPane) FXMLLoader.load(getClass().getResource("Esp_button.fxml"));
if(x == 0 && y == 0)
gp.add(eb, x, y);
else {
gp.addRow(1);
gp.add(eb, x, y);
}
/* children begins with index 1 not 0)*/
Node result = getNodeFromGridPane(eb,5,0);
TextField mac = (TextField) getNodeFromGridPane(eb,1,0);
TextField X = (TextField) getNodeFromGridPane(eb,3,0);
TextField Y = (TextField) getNodeFromGridPane(eb,5,0);
ChangeListener<Object> listener = (MAC, Xi, Yi) ->
printField(mac.getText(), X.getText(), Y.getText());
if(X== null) System.out.println("X is null");
}
private static void printField(String text, String text2, String text3) {
System.out.println("You inserted this: "+"Mac "+ text+ "X "+ text2 +" y "+text3);
}
private Node getNodeFromGridPane(GridPane gridPane, int col, int row) {
for (Node node : gridPane.getChildren()) {
Integer columnIndex = GridPane.getColumnIndex(node);
Integer rowIndex = GridPane.getRowIndex(node);
if(columnIndex != null && rowIndex != null) {
if (columnIndex == col && rowIndex == row) {
return node;
}
}
}
return null;
}
Configuration.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Spinner?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="503.0" prefWidth="631.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.ConfigurationController">
<children>
<Label alignment="CENTER" layoutX="142.0" layoutY="21.0" prefHeight="70.0" prefWidth="382.0" text="CONFIGURATION" textAlignment="CENTER" wrapText="true">
<font>
<Font name="System Bold" size="31.0" />
</font></Label>
<Button fx:id="OKButton" layoutX="260.0" layoutY="423.0" mnemonicParsing="false" onMouseClicked="#OKButtontEvent" prefHeight="31.0" prefWidth="96.0" text="OK" />
<AnchorPane layoutX="225.0" layoutY="94.0" prefHeight="40.0" prefWidth="216.0" />
<HBox layoutX="33.0" layoutY="89.0" prefHeight="326.0" prefWidth="563.0">
<children>
<Label graphicTextGap="6.0" prefHeight="40.0" prefWidth="117.0" text="Numbers of ESP:">
<HBox.margin>
<Insets top="4.5" />
</HBox.margin></Label>
<Spinner fx:id="SpinnerBox" onMouseClicked="#getSpinnerValue" prefHeight="21.0" prefWidth="71.0">
<opaqueInsets>
<Insets top="10.0" />
</opaqueInsets>
<HBox.margin>
<Insets top="8.0" />
</HBox.margin></Spinner>
<GridPane fx:id="gp" prefHeight="52.0" prefWidth="344.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="176.0" minHeight="0.0" prefHeight="0.0" vgrow="SOMETIMES" />
</rowConstraints>
<HBox.margin>
<Insets bottom="220.0" left="30.0" />
</HBox.margin>
</GridPane>
</children>
</HBox>
<Button layoutX="55.0" layoutY="40.0" mnemonicParsing="false" text="<-" />
</children>
</AnchorPane>
ESP_button.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane fx:id="gp" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="36.0" prefWidth="316.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.ButtonController">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="978.0" minWidth="0.0" prefWidth="62.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="978.0" minWidth="0.0" prefWidth="155.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="978.0" minWidth="10.0" prefWidth="27.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="978.0" minWidth="10.0" prefWidth="30.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="811.0" minWidth="0.0" prefWidth="23.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="811.0" minWidth="10.0" prefWidth="35.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="113.0" minHeight="10.0" prefHeight="113.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="29.0" minHeight="0.0" prefHeight="0.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="24.0" minHeight="0.0" prefHeight="0.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<TextField fx:id="mac" prefHeight="29.0" prefWidth="166.0" GridPane.columnIndex="1">
<GridPane.margin>
<Insets right="2.0" />
</GridPane.margin>
</TextField>
<Label prefHeight="22.0" prefWidth="42.0" text="MAC" />
<Label prefHeight="21.0" prefWidth="21.0" text="X:" GridPane.columnIndex="2">
<GridPane.margin>
<Insets left="2.0" />
</GridPane.margin>
</Label>
<Label text="Y:" GridPane.columnIndex="4">
<GridPane.margin>
<Insets left="2.0" />
</GridPane.margin></Label>
<TextField prefHeight="31.0" prefWidth="226.0" GridPane.columnIndex="3">
<GridPane.margin>
<Insets />
</GridPane.margin>
</TextField>
<TextField prefHeight="31.0" prefWidth="226.0" GridPane.columnIndex="5">
<GridPane.margin>
<Insets right="2.0" />
</GridPane.margin>
</TextField>
</children>
<padding>
<Insets left="5.0" />
</padding>
</GridPane>
Я хотел бы вызвать X, Y и mac textProperty (), чтобы присоединить их к слушателю, но я понял, что X, Y и mac нулевые, поэтому я не могу выполнить процедуру.
EspButton:
Конфигурация:
Может ли кто-нибудь помочь мне?