您的
get方法名称错误。根据
PropertyValueFactory文档,如果传入属性名称“
xyz”,则属性值工厂将首先
xyzProperty()在表行中查找属于该对象的方法。如果找不到,将重新寻找一种称为
getXyz()(仔细查看大写字母)的方法,然后将结果包装在中
ReadOnlyObjectWrapper。
因此,以下方法将起作用:
package application;import javafx.beans.property.SimpleIntegerProperty;import javafx.beans.property.SimpleStringProperty;public class Table { private final SimpleIntegerProperty bPlayerID; private final SimpleStringProperty bLeague; private final SimpleStringProperty bName; public Table(int cPlayerID, String cLeague, String cName) { this.bPlayerID = new SimpleIntegerProperty(cPlayerID); this.bLeague = new SimpleStringProperty(cLeague); this.bName = new SimpleStringProperty(cName); } public int getBPlayerID() { return bPlayerID.get(); } public void setBPlayerID(int v) { bPlayerID.set(v); } public String getBLeague() { return bLeague.get(); } public void setBLeague(String v) { bLeague.set(v); } public String getBName() { return bName.get(); } public void setBName(String v) { bName.set(v); }}但是,如
PropertyValueFactory文档中所述,这种情况下的属性将不是“活动的”:换言之,如果值发生更改,表将不会自动更新。此外,如果您想使表可编辑,则在没有进行显式连接以调用set方法的情况下,它不会更新属性。
最好使用“ 属性和绑定”教程中的大纲定义表模型:
package application;import javafx.beans.property.SimpleIntegerProperty;import javafx.beans.property.IntegerProperty;import javafx.beans.property.SimpleStringProperty;import javafx.beans.property.StringProperty;public class Table { private final IntegerProperty bPlayerID; private final StringProperty bLeague; private final StringProperty bName; public Table(int cPlayerID, String cLeague, String cName) { this.bPlayerID = new SimpleIntegerProperty(cPlayerID); this.bLeague = new SimpleStringProperty(cLeague); this.bName = new SimpleStringProperty(cName); } public int getBPlayerID() { return bPlayerID.get(); } public void setBPlayerID(int v) { bPlayerID.set(v); } public IntegerProperty bPlayerIDProperty() { return bPlayerID ; } public String getBLeague() { return bLeague.get(); } public void setBLeague(String v) { bLeague.set(v); } public StringProperty bLeagueProperty() { return bLeague ; } public String getBName() { return bName.get(); } public void setBName(String v) { bName.set(v); } public StringProperty bNameProperty() { return bName ; }}如果这样做,则(在Java 8中)可以使用以下单元格值工厂,而不是
PropertyValueFactory:
aPlayerID.setCellValueFactory(cellData -> cellData.getValue().bPlayerIDProperty());
这将允许编译器捕获任何错误,而不仅仅是在运行时静默失败。



