Java 如何正确使用 PropertyValueFactory?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16377820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use the PropertyValueFactory correctly?
提问by mattbdean
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:
我正在尝试使用 TableView 制作一个表格,并根据User
对象列表以编程方式填充它。将User
有三个变量nameProperty
(字符串), rankProperty
(枚举称为等级),和netMeritsProperty
(INT)。这些都存储在SimpleStringProperty
对象中。我的问题是数据不会出现在实际表中,如下所示:
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);
采纳答案by Alexander Kirov
The answer is about attention, which string content you give as parameter of PropertyValueFactory
, and which methods are implemented in your encapsulated data type.
答案是关于注意力,你将哪些字符串内容作为 的参数PropertyValueFactory
,以及在你封装的数据类型中实现了哪些方法。
Instantiation :
实例化:
new PropertyValueFactory<User, String>("name")
will lookup for :
将查找:
User.nameProperty()
回答by scottb
Using PropertyValueFactory is simple, but it's not foolproof.
使用 PropertyValueFactory 很简单,但并非万无一失。
Probably the single most troublesome thing to using PropertyValueFactory is that the argument you provide must match the name of your JavaFX Bean property according to a STRICT convention:
可能使用 PropertyValueFactory 最麻烦的事情是您提供的参数必须根据 STRICT 约定与您的 JavaFX Bean 属性的名称匹配:
- You must not include the word "Property"
- The matching is case-sensitive, so that "FIRSTNAME" will not be recognized as "firstName"
- 您不得包含“财产”一词
- 匹配区分大小写,因此“FIRSTNAME”不会被识别为“firstName”
The best way is to look at your JavaFX Bean definition block, and find your Property line. Take the name of the property, omit the part that is "property", and copy it exactly (in the same case) to the string that you pass to PropertyValueFactory.
最好的方法是查看您的 JavaFX Bean 定义块,并找到您的属性行。取属性的名称,省略“属性”部分,并将其准确复制(在相同的情况下)到您传递给 PropertyValueFactory 的字符串。
For example, say you have this JavaFX Bean definition:
例如,假设您有这个 JavaFX Bean 定义:
public final String getFirstName() { return this.m_firstname.get(); }
public final void setFirstName(String v) { this.m_firstname.set(v); }
public final StringProperty firstNameProperty() { return m_firstname; }
Look at the name of the property in the last line, "firstNameProperty". Omit "Property", and the resulting string is what you must use -exactly- as your String argument to PropertyValueFactory, eg. "firstName".
查看最后一行中的属性名称,“firstNameProperty”。省略“Property”,结果字符串是您必须使用的 - 完全 - 作为 PropertyValueFactory 的 String 参数,例如。“名”。
tcolMyTableCol.setCellValueFactory(new PropertyValueFactory("firstName"));