2 回答

TA贡献1821条经验 获得超5个赞
虽然违反 Java 命名约定(始终遵循它们!)和 PropertyValueFactory 的不当使用(如果没有令人信服的理由,请不要使用它!)是常见的嫌疑人,但它们似乎不是 OP 问题中的原因。
错误消息表明问题发生在模块化上下文中:
Jun 28, 2019 12:17:35 PM javafx.scene.control.cell.PropertyValueFactory getCellDataReflectively
WARNING: Can not retrieve property 'userId' in PropertyValueFactory:
javafx.scene.control.cell.PropertyValueFactory@638db851 with provided class type: class model.user
java.lang.RuntimeException: java.lang.IllegalAccessException:
module javafx.base cannot access class model.user (in module JavaFXTest)
because module JavaFXTest does not open model to javafx.base
实际上,该示例在非模块化上下文中运行得很好,但在模块化时会失败。错误消息准确地告诉我们该怎么做:打开我们的模块(或其中的一部分)进行反射访问。
Module-info 打开完整的模块或单独的包:
// complete
open module JavaFXTest {
exports ourpackage;
...
}
// parts
module JavaFXTest {
exports ourpackage;
opens ourpackage.model;
}

TA贡献1808条经验 获得超4个赞
这是一个很好的例子来说明“为什么你应该使用Callback而不是PropertyValueFactory.”。目前我看不出有任何理由使用 PVF 而不是 Callback。
在这里您可以看到一个缺点,即您在运行该应用程序之前并没有真正看到您犯了编码错误。由于 PVF 使用反射,如果您没有正确声明它们,它就找不到合适的字段。PVF 期望Property-es 正如您在异常中看到的那样 a是必需的,并且您的班级PropertyRefference中指定的字段都不是esuserProperty
它可以这样使用,但你必须user像这样重写类:
public class User { // use java naming conventions
private IntegerProperty userId;
public int getUserId() { return this.userId.get(); }
public void setUserId(int userId) { this.userId.set(userId); }
public IntegerProperty userIdProperty() { return this.userId; }
private StringProperty userName;
public String getUserName() { return this.userName.get(); }
public void setUserName(String userName) { this.userName.set(userName); }
public StringProperty userNameProperty() {return this.userName; }
public User(int userId,String userName){
this.userId = new SimpleIntegerProperty(userId);
this.userName = new SimpleStringProperty(userName);
}
}
现在,由于字段是属性,PropertyValueFactory 会找到它们,但我不建议使用它,因为如您所见,它可能会导致您在运行它之前甚至没有注意到的问题。
所以不是:
UserId.setCellValueFactory(new PropertyValueFactory<user, String>("userId"));
UserName.setCellValueFactory(new PropertyValueFactory<user, String>("userName"));
使用:
// use java naming conventions (`userId` instead of `UserId`)
userId.setCellValueFactory(data -> data.getValue().userIdProperty().asString());
// same here user naming convention
userName.setCellValueFactory(data -> data.getValue().userNameProperty());
我已经写过几次评论,但我会在这里再次提到它使用java 命名约定。像 User 而不是 user 和 userId 而不是 UserId 等等......
添加回答
举报