javafx无法检索propertyvaluefactory中的属性,即使设置了正确的varproperty()方法

0dxa2lsx  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(528)

这个问题在这里已经有答案了

javafx propertyvaluefactory无法从位于不同包中的类接收属性(2个答案)
5个月前关门了。
总结:
我正在尝试用observablelist填充tableview,observablelist包含“contact”类的对象,这些对象具有simplestringproperty类型的字段。
不幸的是它不起作用。我得到一个错误:

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;
}
nbewdwxp

nbewdwxp1#

很快就解决了,谢谢你!
module-info.java中缺少一行。contact类位于子包“datamodel”中。它需要以下行才能工作:

opens sample.Datamodel;

相关问题