0

I have a list of records and 2 types of TextFields: filled and empty. I am putting these in a VBox.

for (int i = 0; i < records.size(); i++) {
    if (records.get(i).contains("NEW")) {
        TextField fillField = new TextField();
        vbox.getChildren().add(fillField);
    } else {
        TextField filledField = new TextField();
        filledField.setEditable(false);
        filledField.setText(records.get(i));
        vbox.getChildren().add(filledField);
    }
}

After this the user can fill in the free TextFields. How can I update them inside the VBox?

Then I want to check if any of them are empty(how?), in which case I will fill them with "true".

EDIT: So I am doing this:

for (int i = 0; i < vbox.getChildren().size(); i++) {
     if (((TextField) vbox.getChildren().get(i)).getText()==null) {
         TextField filledField = new TextField("true");
         ((TextField) vbox.getChildren().get(i)).setText("true");
         //System.out.println(((TextField)vbox.getChildren().get(i)).getText()); 
     }
}

My problem is that when I am printing in the console, I do see true when the field is empty. But in my application, the field remains empty. Do I need to update vbox or something, after I update all the fields or?

fabian
  • 80,457
  • 12
  • 86
  • 114
ddd
  • 13
  • 5
  • Step 1: Get the `TextField`. Step 2: Access the `text` property of the `TextField` to do the check/replacement and apply other modifications, if necessary... It would help potential answerers, if you explain which of those parts you're having problems with... – fabian Jun 11 '18 at 23:56
  • @fabian I did that but it still doesn't work and I am not sure why. I updated my question with what I am doing. – ddd Jun 12 '18 at 00:26
  • https://stackoverflow.com/questions/50690240/extracting-text-from-dynamically-created-textfields-inside-various-rows-of-gridp/50692626#comment88422024_50692626 – SedJ601 Jun 12 '18 at 01:39

1 Answers1

0

The text of a TextField only becomes null, if you set the property to this value. This is a bad idea though, since you'd need to check for null and the empty string (the latter being the result of adding some chars and deleting them from the TextField).

In this case the simplest solution would be not to do this and use String.isEmpty for the checks:

for (String record : records) {
    TextField textField = new TextField();
    if (!record.contains("NEW")) {
        textField.setEditable(false);
        textField.setText(record);
    }
    vbox.getChildren().add(textField);
}
for (Node child : vbox.getChildren()) {
    TextField tf = (TextField) child;
    if (tf.getText().isEmpty()) {
        tf.setText("true");
    }
}
fabian
  • 80,457
  • 12
  • 86
  • 114