0

This is my code:

for (int i=1; i<= columnCount; i++) {
    HBox h = new HBox();
    Label column = new Label(rsmd.getColumnName(i));
    TextField entry = new TextField();
    h.getChildren().addAll(column, entry);
    dialogVbox.getChildren().add(h);
}

I need to run this in a for loop because the number of rows created is dependent on the columns retrieved from a database table. After this when the user clicks a button I need to retrieve the new values they have entered in the "entry" TextFields. Is there any way to do this? I'm assuming I may have to create some dynamic list outside of the loop and populate it, but I'm not sure how to do that. Any help would be appreciated!

guleryuz
  • 2,714
  • 1
  • 15
  • 19

1 Answers1

-1

why not using a map populated directly on edit actions of corresponding TextFields:

Map<String, String> newValues = new HashMap<>();

for (int i=1; i<= columnCount; i++) {
    final String columnName = rsmd.getColumnName(i); 
    HBox h = new HBox();
    Label column = new Label(columnName);
    TextField entry = new TextField();
    entry.textProperty().addListener((e,o,n) -> newValues.put(columnName, n));
    h.getChildren().addAll(column, entry);
    dialogVbox.getChildren().add(h);
}

then insert db

String columns = newValues.entrySet().stream().map( n -> n.getKey() ).collect( Collectors.joining( "," ) );
String values = newValues.entrySet().stream().map( n -> n.getValue() ).collect( Collectors.joining( "," ) );

String sql = "insert into table ( " + columns + ") values (" + values + ")";
// run sql
guleryuz
  • 2,714
  • 1
  • 15
  • 19
  • Okay this makes sense, having a listener on each of the TextFields to respond with the new values and update a list makes sense. I didn't know it was possible to create a listener within a loop, but I suppose there's no reason you couldn't.. Thanks! – Tony Geleynse Aug 30 '18 at 20:06