JavaFx: ¿Cómo actualizar el texto de TextFields creados dinámicamente dentro de GridPane?
Estoy desarrollando una aplicación en JavaFx, en la que estoy creando dinámicamente TextFeids y CheckBoxes dentro de GridPane de esta manera:
Estoy agregando números que el usuario ingresará en TextField 1 y TextField 2 y los mostraré en TextField 3 de esta manera usando oyente:
Problema: Lo que quiero es que cuando el usuario verifique el CheckBox, debería agregar 10 al valor ya presente en el TextField 3 de(misma fila que el CheckBox activado) y actualice el texto de TextField 3, y cuando el usuario desmarque CheckBox, debería restar 10 del valor presente en TextField 3 de(misma fila que el CheckBox activado) y actualizo el texto de TextField 3. Intenté hacerlo pero no funciona (no agrega ni elimina):
Así es como estoy creando GridPane:
public static GridPane table(int rows){
GridPane table = new GridPane();
for(int i=0; i<rows; i++){
TextField textField = new TextField();
textField.setAlignment(Pos.CENTER);
TextField textField2 = new TextField();
textField2.setAlignment(Pos.CENTER);
CheckBox checkBox = new CheckBox("Check Box");
checkBox.setTextFill(Color.WHITE);
checkBox.setAlignment(Pos.CENTER);
TextField textField3 = new TextField();
textField3.setAlignment(Pos.CENTER);
table.add(textField, 0, i);
table.add(textField2, 1, i);
table.add(checkBox , 2, i);
table.add(textField3,3, i);
GridPane.setMargin(textField, new Insets(5));
GridPane.setMargin(textField2, new Insets(5));
GridPane.setMargin(checkBox, new Insets(5));
GridPane.setMargin(textField3, new Insets(5));
}
table.setAlignment(Pos.CENTER);
return table;
}
Un método para devolver un componente de la tabla en una fila y columna específicas
public static Node getComponent (int row, int column, GridPane table) {
for (Node component : table.getChildren()) {
if(GridPane.getRowIndex(component) == row &&
GridPane.getColumnIndex(component) == column) {
return component;
}
}
return null;
}
Así es como estoy agregando:
public void add(GridPane table, int numRows){
for(int i=0; i<numRows; i++){
try {
int valueA = Integer.parseInt(((TextField)(getComponent (i, 0, table))).getText());
System.out.println(valueA);
int valueB = Integer.parseInt(((TextField)(getComponent (i, 1, table))).getText());
System.out.println(valueB);
int add = valueA+valueB;
String addToString = Integer.toString(add);
((TextField)(getComponent (i, 3, table))).setText(addToString);
} catch (NullPointerException e) {
System.out.print("Caught the NullPointerException");
}
}
}
Problema: Suma y resta usando CheckBox:
public void addPause(GridPane table, int numRows){
for(int i=0; i<numRows; i++){
boolean pause = ((CheckBox)(getComponent (i, 2, table))).isSelected();
int getTextValue = Integer.parseInt(((TextField)(getComponent (i, 3, table))).getText());
int addPause = getTextValue+10;
int removePause = addPause-10;
String addPToString = Integer.toString(addPause);
String removePToString = Integer.toString(removePause);
if (pause,) {
((TextField)(getComponent (i, 3, table))).setText(addPToString);
} else {
((TextField)(getComponent (i, 3, table))).setText(removePToString);
}
}
}
Así es como estoy activando el uso de oyentes:
for(Node node : table.getChildren()){
if(node instanceof TextField){
((TextField)node).textProperty().addListener((obs, old, newV)->{
add(table, numRows);
});
}
else if(node instanceof CheckBox){
((CheckBox)node).selectedProperty().addListener((obs, old, newV)->{
addPause(table, numRows);
});
}
}