I'm having trouble figuring out how to catch an exception in the setOnEditCommit()
function.
TableColumn<Property, Float> value_clm = new TableColumn<Property, Float>("Value");
value_clm.setCellValueFactory(new PropertyValueFactory<Property, Float>("value"));
value_clm.setCellFactory(TextFieldTableCell.forTableColumn(new FloatStringConverter()));
value_clm.setSortable(false);
value_clm.setOnEditCommit(
new EventHandler<CellEditEvent<Property, Float>>() {
public void handle(CellEditEvent<Property, Float> t) {
try{
((Property) t.getTableView().getItems().get(t.getTablePosition().getRow())).setValue(t.getNewValue());
saved = false;
}catch(NumberFormatException e){
((Property) t.getTableView().getItems().get(t.getTablePosition().getRow())).setValue(t.getOldValue());
}
}
}
);
See what happens is that if the user types a non-numeric character except for "." to the text field and press enter
a NumberFormatException
gets thrown, so I tried to do a try/catch to handle the exception when its thrown, but the same thing happens when the user presses enter
. I don't know how to fix this, but it seems like the newValue
gets checked if its a float
before firing the handle()
function. Also the goal is to make it so that user cannot type a non-numeric character except for "." into the text field.
If any of you guys can help I would really appreciate it.