JavaFX Properties in TableView
One somewhat strange part of the property API is that IntegerProperty
implements ObservableValue<Number>
, not ObservableValue<Integer>
. So, somewhat counterintuitively, you need
TableColumn<Person, Number> ageColumn = new TableColumn<Person, Number>("Age");
As an aside, and I'm not sure if this causes problems, but it's more usual to use int
instead of Integer
for the return type for the get method and the parameter type for the set method in your model class. These match the types for IntegerProperty.get()
and IntegerProperty.set(...)
, so it just avoids some implicit auto (un)boxing.
you can use .asObject Method to make your code work
it doesn't need you to change the TableColumn Paramater to Number
ageColumn.setCellValueFactory(cellData -> cellData.getValue().ageProperty().asObject());
idk which one is better code,
change the parameter to Number or use asObject Method
related Link
For String type :
firstNameColumn.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty());
When you want to use IntegerProperty
or DoubleProperty
, the setCellValueFactory(...)
must have an additional asObject()
:
ageColumn.setCellValueFactory(cellData -> cellData.getValue().ageProperty().asObject());
This is necessary because of a bad design decision of JavaFX.
Source and credits