I am adapting a code to fill a TablaView (javaFX) IT WORKS but there is a part of the code that I do not really know how it works, I would need help if someone can explain that part of the code to me and if I know how to add the following requirement: Let a column show double with two decimals and at the same time check that only numbers are entered.
//Asi declaro mi tabla:
@FXML
private TableView<Personal> myTable;
@FXML
private TableColumn<Personal,Integer> columnaId;
@FXML
private TableColumn<Personal,String> columnaNombre;
@FXML
private TableColumn<Personal,String> columnaDNI;
@FXML
private TableColumn<Personal,Double> columnaMonto;
// Asi le asigno las celdas:
myTable.setEditable(true); //Permito la edicion
columnaId.setCellValueFactory(new PropertyValueFactory<Personal, Integer>("Id"));
columnaNombre.setCellValueFactory(new PropertyValueFactory<Personal, String>("Nombre"));
columnaDNI.setCellValueFactory(new PropertyValueFactory<Personal, String>("Dni"));
columnaMonto.setCellValueFactory(new PropertyValueFactory<Personal, Double>("monto"));
So I know what is happening, but often I have the following code that I do not quite understand what it does and what it would believe to be where I have to apply the logic for my requirement.
columnaMonto.setCellFactory(TextFieldTableCell.forTableColumn(new DoubleStringConverter()));
columnaMonto.setOnEditCommit(
new EventHandler<TableColumn.CellEditEvent<Personal, Double>>() {
@Override
public void handle(TableColumn.CellEditEvent<Personal, Double> event) {
((Personal)event.getTableView().getItems().get(
event.getTablePosition().getRow())
).setMonto(event.getNewValue());
actualizarTotales(); //Un simple método donde sumo los totales y los muestro en un Text
}
}
);
1-Can someone explain to me in simple words the why of setCellFactory?
2-How can I control that numeric characters are entered in the Amount column?
3-Let the value of Amount take only two decimal places.
Thank you!