简体   繁体   中英

retrieve data from multi text field in javafx

i'm working on an application where i should retrieve data from text fields that i created dynamically in a javaFX controller after that the user decides the number of the text fields. since in the first view i have only one textfield, i did retrieve it easily with no problem but i just have no clue about how to do it when the text field are dynamically created from the controller.

i tried to use the textfield.getText(); but it's not working.

this is my code:

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);
    }

here in this Handlevent in the same controller i'm trying to retrieve the data in order to send it to the logic part of my project.

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      
    }

the button that takes those actions is in the FXML file.

Something like :

       TextField textField[] = new TextField[...]; 

      for(int n=1; n<=i;n++) {
            textField[n] = new TextField("input "+n);
            gridPane.getChildren().add(textField[n]);
        }

You can test by printing them using .getText()

    for(int n=1; n<=i;n++) {System.out.println(textField[n].getText());}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM