1

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.

  • I don't really understand the problem you are having, but try looking at http://stackoverflow.com/questions/31039449/java-8-u40-textformatter-javafx-to-restrict-user-input-only-for-decimal-number . – Itai Jul 09 '16 at 08:55
  • just catch all exceptions? problem, no ? then replace NumberFormatException with Exception – Abdullah Asendar Jul 09 '16 at 09:22
  • The best solution would be to implement your own `TextFieldTableCell`, something like [here](http://stackoverflow.com/questions/34698986/cancel-the-modification-of-a-tableview-cell). There you can implement any custom logic you require and use `TextFormatter` as was suggested. – whitesite Jul 09 '16 at 09:31
  • Instead of ```((Property) t.getTableView().getItems().get(t.getTablePosition().getRow()))``` just use ```t.getRowValue()``` – Jorn Vernee Jul 09 '16 at 14:41

1 Answers1

0

The answer is to implement your own string to float converter, since the default one pretty much just uses Float.valueOf, without handling the exceptions.


value_clm.setCellFactory(TextFieldTableCell.forTableColumn(new FloatStringConverter()));

Becomes:

value_clm.setCellFactory(TextFieldTableCell.forTableColumn(new FloatStringConverter() {
        @Override
        public Float fromString(String value) {
            try {
                return super.fromString(value);
            } catch(NumberFormatException e) {
                return Float.Nan; // An abnormal value
            }
        }
    }));

You can then check for this abnormal value:

value_clm.setOnEditCommit(t -> {            
        if(t.getNewValue().isNaN()) {
            t.getRowValue().setValue(t.getOldValue());
        } else {
            t.getRowValue().setValue(t.getNewValue());
        }
    });
Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93