【问题标题】:JavaFX cannot retrieve property in PropertyValueFactory even though correct varProperty() methods are set [duplicate]即使设置了正确的 varProperty() 方法,JavaFX 也无法检索 PropertyValueFactory 中的属性[重复]
【发布时间】:2020-08-20 10:58:09
【问题描述】:

总结:

我正在尝试使用 ObservableList 填充 TableView,其中包含具有 SimpleStringProperty 类型字段的“Contact”类对象。

不幸的是,它不起作用。我得到了错误:

WARNING: Can not retrieve property 'firstName' in PropertyValueFactory: javafx.scene.control.cell.PropertyValueFactory@61e63bbc with provided class type: class sample.Datamodel.Contact
java.lang.RuntimeException: java.lang.IllegalAccessException: module javafx.base cannot access class sample.Datamodel.Contact (in module JavaFXChallengeContactList) because
at javafx.base/com.sun.javafx.property.PropertyReference.getProperty(PropertyReference.java:199)
at javafx.controls/javafx.scene.control.cell.PropertyValueFactory.getCellDataReflectively(PropertyValueFactory.java:182)
... and many more

我对这个主题进行了广泛的研究。在许多情况下,其他人没有在类中实现 variableProperty() 方法。但它就在那里。

我已经创建了一个创建列的方法,但是其他尝试(没有它并将代码直接放在 initialize() 方法中)也没有奏效。

当我运行代码时,我得到带有标题的表格,但没有数据。

这里有什么问题?

非常感谢您的支持!

联系人类中的代码:

public class Contact {

private SimpleStringProperty firstName;
private SimpleStringProperty lastName;
private SimpleStringProperty phoneNumber;
private SimpleStringProperty notes;

public Contact(SimpleStringProperty firstName, SimpleStringProperty lastName, SimpleStringProperty phoneNumber, SimpleStringProperty notes) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.phoneNumber = phoneNumber;
    this.notes = notes;
}

public String getFirstName() {
    return firstName.get();
}

public SimpleStringProperty firstNameProperty() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName.set(firstName);
}

... (other methods for lastName, phoneNumer and notes are identical)

ContactData 类中的代码:

public class ContactData {

private ObservableList<Contact> contacts;

public ContactData() {
    this.contacts = FXCollections.observableArrayList();
}

public void addContact(String fName, String lName, String phone, String notes) {
    SimpleStringProperty firstName = new SimpleStringProperty(fName);
    SimpleStringProperty lastName = new SimpleStringProperty(lName);
    SimpleStringProperty phoneNumber = new SimpleStringProperty(phone);
    SimpleStringProperty yourNotes = new SimpleStringProperty(notes);
    this.contacts.add(new Contact(firstName, lastName, phoneNumber, yourNotes));

}

public ObservableList<Contact> getContacts() {
    return contacts;
}

控制器中的代码:

public class Controller {

@FXML
private TableView table;

public void initialize() {
    ContactData contactDatabase = new ContactData();
    contactDatabase.addContact("John", "Doe", "123", "Hi John");
    contactDatabase.addContact("Peter", "Mustang", "321", "Hello Pete");
    contactDatabase.addContact("Carl", "Mueller", "555", "Hey Carl");


    TableColumn firstCol = setUpColumn("First Name","firstName");
    TableColumn secondCol = setUpColumn("Last Name","lastName");
    TableColumn thirdCol = setUpColumn("Phone Number","phoneNumber");
    TableColumn lastCol = setUpColumn("Your Notes","notes");

    table.setItems(contactDatabase.getContacts());
    table.getColumns().addAll(firstCol, secondCol, thirdCol, lastCol);

}

public TableColumn setUpColumn(String columnName, String idInContact) {
    TableColumn<Contact, SimpleStringProperty> column = new TableColumn<>(columnName);
    column.setCellValueFactory(new PropertyValueFactory<>(idInContact));
    return column;
}

【问题讨论】:

  • 错误信息不完整:本例中的重要部分是java.lang.RuntimeException: java.lang.IllegalAccessException: module javafx.base cannot access class sample.Datamodel.Contact(在模块 JavaFXChallengeContactList) 因为 after because 的文本会告诉你确切的问题:) 确保联系人的包已导出或至少在你的模块信息中打开
  • 是的!非常感谢,成功了!解决方案是在 module-info.java 中添加一行:opens sample.Datamodel;
  • 注意一个很多更好的解决方案是避免使用PropertyValueFactory。使用正确类型的表列(即TableColumn&lt;Contact, String&gt;,而不是原始类型TableColumn),然后您可以使用firstCol.setCellValueFactory(cellData -&gt; cellData.getValue().firstNameProperty()) 等。

标签: java javafx illegalaccessexception


【解决方案1】:

很快就解决了,谢谢kleopatra!

module-info.java 中缺少一行。 Contact 类位于子包“Datamodel”中。它需要以下行才能使其工作:

opens sample.Datamodel;

【讨论】:

  • 很高兴它正在工作 :) 请注意,如果可能的话,最好不要使用 PropertyValueFactory - 您的模型类公开属性,您可以直接使用它们
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-24
  • 1970-01-01
  • 2013-01-19
  • 2012-01-18
  • 1970-01-01
  • 2021-01-03
  • 1970-01-01
相关资源
最近更新 更多