Cancelar la modificación de una celda TableView
Me gustaría tener un ejemplo que me explique cómo puedo cancelar una edición y restablecer el valor anterior de una celda específica en un TableView que fue editado pero no pudo pasar la validación. Vea el código a continuación para más información.
tcAantalDagen.setOnEditCommit(cell -> {
int dagen = Integer.parseInt(cell.getNewValue());
if (Integer.parseInt(cell.getNewValue()) < 1 || Integer.parseInt(cell.getNewValue()) > 31) {
// This shows an Alert Dialog
Main.toonFoutbericht("Het item kan maar tussen 1 en 31 dagen uitgeleend worden");
// The "return;" is successful in canceling the passing through of the new value of the cell to the backend code,
// but in the TableView the new value is still set in the cell, which can confuse the user
return;
}
}
El valor de la celda se establece así:
// getAantalDagen() returns an Integer
tcAantalDagen.setCellValueFactory(cell -> {
return new ReadOnlyObjectWrapper<String>(Integer.toString(cell.getValue().getAantalDagen()));
});
// Make the cells of the tcAantalDagen column editable
tcAantalDagen.setCellFactory(TextFieldTableCell.forTableColumn());
// The table needs to be set to editable too for the above column to work
tblUitleningen.setEditable(true);