Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

java - 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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...