我正在开发一个应用程序,在用户决定文本字段的数量之后,我应该从我在 javaFX Controller 中动态创建的文本字段中检索数据。因为在第一个 View 中我只有一个文本字段,所以我确实很容易地检索它,没有任何问题,但我只是不知道当从 Controller 动态创建文本字段时如何执行此操作。
我尝试使用textfield.getText();但它不起作用。
这是我的代码:
public void handleButtonActionNext(ActionEvent event) throws IOException{
String t = textField.getText();
IntegerStringConverter a = new IntegerStringConverter();
this.i = a.fromString(t);
Node node = (Node) event.getSource();
Stage stage = (Stage) node.getScene().getWindow();
Scene scene = stage.getScene();
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("ProcessusTimeExec.fxml"));
Parent root = (Parent) fxmlLoader.load();
scene.setRoot(root);
gridPane = (GridPane) FXMLLoader.load(getClass().getResource("ProcessusTimeExec.fxml"));
Scene secondScene = new Scene(gridPane);
gridPane.setPadding(new Insets(10, 10, 10, 10));
this.listTextField = new ArrayList<>();
for(int n=1; n<=i;n++){
this.label = new Label("execution Time "+n);
GridPane.setConstraints(label, 0, n+2);
GridPane.setConstraints(textField, 1, n+2);
gridPane.getChildren().add(textField);
gridPane.getChildren().add(label);
}
stage.setScene(secondScene);
}
在同一个 Controller 的 Handlevent 中,我尝试检索数据,以便将其发送到项目的逻辑部分。
public void handleButtonActionAlgo(ActionEvent event) throws IOException{
Node node = (Node) event.getSource();
Stage stage = (Stage) node.getScene().getWindow();
Scene scene = stage.getScene();
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Algo.fxml"));
Parent root = (Parent) fxmlLoader.load();
scene.setRoot(root);
// retrieve the data from the textFields
}
执行这些操作的按钮位于 FXML 文件中。
最佳答案
类似于:
TextField textField[] = new TextField[...];
for(int n=1; n<=i;n++) {
textField[n] = new TextField("input "+n);
gridPane.getChildren().add(textField[n]);
}
您可以使用 .getText() 打印它们来进行测试
for(int n=1; n<=i;n++) {System.out.println(textField[n].getText());}
关于java - 从 javafx 中的多文本字段检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26978566/