Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the PropertyValueFactory correctly?

I am trying to make a table with a TableView and fill it programmatically based on a list of User objects. The User has three variables, nameProperty (String), rankProperty (Enum called Rank), and netMeritsProperty (int). These are all stored in SimpleStringProperty objects. My problem is that the data will not appear in the actual table, as shown here:

Here is my code for the table. What am I not understanding?

TableColumn<User, String> name = new TableColumn<>("Name");
name.setCellValueFactory(new PropertyValueFactory<User, String>("nameProperty"));

TableColumn<User, String> rank = new TableColumn<>("Rank");
rank.setCellValueFactory(new PropertyValueFactory<User, String>("rankProperty"));

TableColumn<User, String> netMerits = new TableColumn<>("Net Merits");
netMerits.setCellValueFactory(new PropertyValueFactory<User, String>("netMeritsProperty"));

userTable.getColumns().addAll(name, rank, netMerits);
like image 923
mattbdean Avatar asked May 04 '13 18:05

mattbdean


People also ask

What is PropertyValueFactory?

public PropertyValueFactory(String property) Creates a default PropertyValueFactory to extract the value from a given TableView row item reflectively, using the given property name. Parameters: property - The name of the property with which to attempt to reflectively extract a corresponding value for in a given object.

What does setCellValueFactory do?

setCellValueFactory. Sets the value of the property cellValueFactory.

What is Setcellfactory?

The cell factory for all cells in this column. The cell factory is responsible for rendering the data contained within each TableCell for a single table column.


1 Answers

The answer is about attention, which string content you give as parameter of PropertyValueFactory, and which methods are implemented in your encapsulated data type.

Instantiation :

new PropertyValueFactory<User, String>("name")

will lookup for :

User.nameProperty()
like image 50
Alexander Kirov Avatar answered Sep 24 '22 00:09

Alexander Kirov