JavaFX无法在PropertyValueFactory中检索属性,即使正确的varProperty()方法已设置

huangapple go评论115阅读模式
英文:

JavaFX cannot retrieve property in PropertyValueFactory even though correct varProperty() methods are set

问题

**摘要**

我正在尝试使用包含SimpleStringProperty类型字段的Contact类对象的ObservableList来填充TableView

不幸的是它不起作用我得到了以下错误

    警告无法在PropertyValueFactory中检索属性'firstName':javafx.scene.control.cell.PropertyValueFactory@61e63bbc的提供的类类型为class sample.Datamodel.Contact
    java.lang.RuntimeException: java.lang.IllegalAccessException: 模块javafx.base无法访问类sample.Datamodel.Contact位于模块JavaFXChallengeContactList),
    在javafx.base/com.sun.javafx.property.PropertyReference.getProperty(PropertyReference.java:199)    在javafx.controls/javafx.scene.control.cell.PropertyValueFactory.getCellDataReflectively(PropertyValueFactory.java:182)    ... 以及更多

我在这个问题上进行了广泛的研究在许多情况下其他人没有在类中实现variableProperty()方法但是这个方法是存在的

我创建了一个方法来创建列但其他尝试没有这个方法并且直接将代码放在initialize()方法中也没有起作用

当我运行代码时表格会显示标题但没有数据

这里有什么问题

非常感谢您的支持

**Contact类中的代码**

```java
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);
    }

    ...其他lastNamephoneNumer和notes的方法相同
}

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<Contact> 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<Contact, String> firstCol = setUpColumn("First Name","firstName");
        TableColumn<Contact, String> secondCol = setUpColumn("Last Name","lastName");
        TableColumn<Contact, String> thirdCol = setUpColumn("Phone Number","phoneNumber");
        TableColumn<Contact, String> lastCol = setUpColumn("Your Notes","notes");

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

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

<details>
<summary>英文:</summary>
**Summary:**
I am trying to populate a TableView with an ObservableList that contains objects of class &quot;Contact&quot; that have fields of type SimpleStringProperty.
Unfortunately it doesn&#39;t work. I get the error:
WARNING: Can not retrieve property &#39;firstName&#39; 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
I did extensive research on this topic. In many cases other people have not implemented the variableProperty() method in the class. But it is there.
I have made a method to create the columns, but other attempts (without it and putting the code directly in the initialize() method) also didn&#39;t work.
When I run the code, I get the table with the headers, but no data.
What is wrong here? 
Thank you very much for your support!
**Code in Contact Class:**
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)
**Code in ContactData Class:**
public class ContactData {
private ObservableList&lt;Contact&gt; 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&lt;Contact&gt; getContacts() {
return contacts;
}
**Code in Controller:**
public class Controller {
@FXML
private TableView table;
public void initialize() {
ContactData contactDatabase = new ContactData();
contactDatabase.addContact(&quot;John&quot;, &quot;Doe&quot;, &quot;123&quot;, &quot;Hi John&quot;);
contactDatabase.addContact(&quot;Peter&quot;, &quot;Mustang&quot;, &quot;321&quot;, &quot;Hello Pete&quot;);
contactDatabase.addContact(&quot;Carl&quot;, &quot;Mueller&quot;, &quot;555&quot;, &quot;Hey Carl&quot;);
TableColumn firstCol = setUpColumn(&quot;First Name&quot;,&quot;firstName&quot;);
TableColumn secondCol = setUpColumn(&quot;Last Name&quot;,&quot;lastName&quot;);
TableColumn thirdCol = setUpColumn(&quot;Phone Number&quot;,&quot;phoneNumber&quot;);
TableColumn lastCol = setUpColumn(&quot;Your Notes&quot;,&quot;notes&quot;);
table.setItems(contactDatabase.getContacts());
table.getColumns().addAll(firstCol, secondCol, thirdCol, lastCol);
}
public TableColumn setUpColumn(String columnName, String idInContact) {
TableColumn&lt;Contact, SimpleStringProperty&gt; column = new TableColumn&lt;&gt;(columnName);
column.setCellValueFactory(new PropertyValueFactory&lt;&gt;(idInContact));
return column;
}
</details>
# 答案1
**得分**: 3
那个问题很快就解决了,谢谢 kleopatra!
在 module-info.java 文件中缺少了一行代码。Contact 类位于子包 "Datamodel" 中。需要添加以下行使其正常工作:
opens sample.Datamodel;
<details>
<summary>英文:</summary>
That was solved quickly, thank you kleopatra!
a line in module-info.java was missing. The Contact class is located in the subpackage &quot;Datamodel&quot;. It required the following line to make it work:
opens sample.Datamodel;
</details>

huangapple
  • 本文由 发表于 2020年8月20日 18:58:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63503607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定