TableView不显示TableColumns

时间:2018-12-03 23:44:32

标签: java javafx

我正在做一个JavaFX应用程序,查看表中数据的结果非常不一致。我的代码如下:

@FXML
private TableColumn colName, colSurname, colAge, colID, colTF, colEmail;

private void initializeTable() {
    colName.setCellValueFactory(new PropertyValueFactory<Person, String>("name"));
    colSurname.setCellValueFactory(new PropertyValueFactory<Person, String>("surname"));
    colAge.setCellValueFactory(new PropertyValueFactory<Person, Integer>("age"));
    colID.setCellValueFactory(new PropertyValueFactory<Person, String>("dni")); //why this doesn't work????
    colTF.setCellValueFactory(new PropertyValueFactory<Person, String>("tf")); //why this doesn't work????
    colEmail.setCellValueFactory(new PropertyValueFactory<Person, String>("email"));
    // ...
}

Person类是:

public class Person {
    public SimpleStringProperty name, surname, dni, tf, email;
    public SimpleIntegerProperty age;

    public Person(String name, String surname, int age, String dni, String tf, String email) {
        this.name = new SimpleStringProperty(name);
        this.surname = new SimpleStringProperty(surname);
        this.dni = new SimpleStringProperty(dni);
        this.tf = new SimpleStringProperty(tf);
        this.email = new SimpleStringProperty(email);
        this.age = new SimpleIntegerProperty(age);
    }

    public String getName() {return name.get();}
    public String getSurname() {return surname.get();}
    public Integer getAge() {return age.get();}
    public String getDNI() {return dni.get();}
    public String getTF() {return tf.get();}
    public String getEmail() {return email.get();}
}

该表显示姓名姓氏年龄电子邮件,但不显示 dni < / strong>或 tf 。为什么?

1 个答案:

答案 0 :(得分:0)

谢谢@fabian帮助我找到答案。

问题出在@James_D说的here

  

get方法的名称错误。根据PropertyValueFactory文档,如果传入属性名称“ xyz”,则属性值工厂将首​​先在表行中查找属于该对象的方法xyzProperty()。如果没有找到,它将重新寻找一种名为getXyz()的方法(仔细查看该处的大小写),然后将结果包装在ReadOnlyObjectWrapper中。

因此解决方案是将Person的getters中的大小写更改为:

    public String getDni() {return dni.get();}
    public String getTf() {return tf.get();}