javafx PropertyValueFactory


Example

PropertyValueFactory can be used as cellValueFactory in a TableColumn. It uses reflection to access methods that match a certain pattern to retrieve the data from a TableView item:

Example

TableColumn<Person, String> nameColumn = ...
PropertyValueFactory<Person, String> valueFactory = new PropertyValueFactory<>("name");
nameColumn.setCellValueFactory(valueFactory);

The name of the method that is used to get the data depends on the constructor paramerter for PropertyValueFactory.

  • Property method: This kind of method is expected to return a ObservableValue containing the data. Changes can be observed. They need to match the pattern <constructor parameter>Property and take no parameters.
  • Getter method: This kind of method expects to return the value directly (String in the above example). The method name needs to match the pattern get<Constructor parameter>. Note that here <Constructor parameter> begins with a uppercase letter. This method shouldn't take parameters.

Sample names of methods

constructor parameter (without quotes)name of property methodname of getter method
foofooPropertygetFoo
fooBarfooBarPropertygetFooBar
XYZXYZPropertygetXYZ
listIndexlistIndexPropertygetListIndex
aValueaValuePropertygetAValue