Editing a Number cell in a TableView
TextFieldTableCell is type parameterized and has a stringConverter
property that you can use to convert to/from String and your desired type.
Try something like:
TextFieldTableCell.<BMIRecord, Number>forTableColumn(new NumberStringConverter())
NumberStringConverter has some additional constructors for specifying the formatting, see the javadocs.
Here's a more complete example:
public class Person {
public Person(String name0, int age0) {
name = name0;
age = age0;
}
public String name;
public int age;
}
TableView<Person> personTable = new TableView<>();
TableColumn<Person, Number> age = new TableColumn<>();
age.setCellValueFactory(new Callback<CellDataFeatures<Person, Number>, ObservableValue<Number>>() {
@Override
public ObservableValue<Number> call(CellDataFeatures<Person, Number> p) {
return new SimpleIntegerProperty(p.getValue().age);
}
});
age.setCellFactory(TextFieldTableCell.<Person, Number>forTableColumn(new NumberStringConverter()));
This does not work well, though, because NumberStringConverter is so, to be blunt, badly implemented that it simply throws a ParseException
at you if you happen to enter a string instead of a number in the cell.
However it should be relatively trivial to implement your own string converter, where you could also do some simple validation (e.g. value should be between 0 and 100).