英文:
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);
}
...(其他lastName、phoneNumer和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 "Contact" that have fields of type SimpleStringProperty.
Unfortunately it doesn't work. I get the error:
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
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'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<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;
}
**Code in Controller:**
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;
}
</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 "Datamodel". It required the following line to make it work:
opens sample.Datamodel;
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论