即使我创建了一个ObservableList,TableRows也是空的

时间:2014-07-22 19:12:11

标签: hibernate javafx scenebuilder

我有一个名为Student的Hibernate ORM类,我想向tableview显示。我现在面临的问题是行是空的,我不知道为什么。这是我尝试解决问题的方法:

@FXML
private TableView<Student> studentTbl;
@FXML
private TableColumn <Student, String>cID;
private ObservableList<Student> studentObservableList = FXCollections.observableArrayList(entityFacadeClass.getAllStudents());
ArrayList<Student> students;

 @FXML
public void populateStudentTable(){
    if(students == null){
   students = entityFacadeClass.getAllStudents();
    }
    studentTbl.setItems(studentObservableList);
    for(Student student : students){
        cID.setCellValueFactory(new PropertyValueFactory<Student, String>(student.getStudentPin()));
    }
    studentTbl.getColumns().setAll(cID);


}

实体班学生:

@Entity
@NamedQueries(
        @NamedQuery(name = "Student.getAllStudent", query = "select s from Student s")
)
@Table(name="student")
public class Student {

    private String _studentPin;
    private String _studentName;
    private String _studentAddress;
    private String _studentPhone;

    public Student(){

    }

    public Student(String studentPin, String studentName, String studentAddress, String studentPhone ){
        this._studentPin = studentPin;
        this._studentName = studentName;
        this._studentAddress = studentAddress;
        this._studentPhone = studentPhone;
    }
    @Id
    @Column(name="s_pin")
    @GeneratedValue(strategy=GenerationType.AUTO)
    public String getStudentPin() {
        return _studentPin;
    }

    public void setStudentPin(String studentPin) {
        this._studentPin = studentPin;
    }
    @Column(name="s_name")
    public String getStudentName() {
        return _studentName;
    }

    public void setStudentName(String studentName) {
        this._studentName = studentName;
    }
    @Column(name="s_address")
    public String getStudentAddress() {
        return _studentAddress;
    }

    public void setStudentAddress(String studentAddress) {
        this._studentAddress = studentAddress;
    }
    @Column(name="s_phone")
    public String getStudentPhone() {
        return _studentPhone;
    }

    public void setStudentPhone(String studentPhone) {
        this._studentPhone = studentPhone;
    }


}

任何线索我做错了什么?我也没有收到任何错误。

1 个答案:

答案 0 :(得分:1)

PropertyValueFactoryCallback,通过反思来运作。 PropertyValueFactory构造函数的参数是用于计算要显示的值的属性的名称,而不是要显示的实际值。

所以,如果你这样做

cID.setCellValueFactory(new PropertyValueFactory<Student, String>("studentPin"));

然后对于表中的每个(已填充的)行,表视图将查看该行中的Student并调用getStudentPin()以确定要显示的值。

对于populateStudentTable()方法,您只需要

@FMXL
public void populateStudentTable() {
    studentTbl.setItems(studentObservableList);
    cID.setCellValueFactory(new PropertyValueFactory<Student, String>("studentPin"));
}

第二行(cID.setCellValueFactory(...))应该采用initialize()方法(或者您甚至可以直接在FXML中执行此操作)。

您的students列表看起来多余:如果您需要访问数据,可以使用studentObservableList

相关问题