Reputation: 699
I have an Hibernate ORM class called Student that I want to show to a tableview. The problem I face now is that the rows are empty and I don't really know why. This is my attempt to solve the problem:
@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);
}
And the entity-class Student:
@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;
}
}
Any clues what I'm doing wrong? I'm not getting any error either.
Upvotes: 0
Views: 111
Reputation: 209684
The PropertyValueFactory
is a Callback
that works by reflection. The argument to the PropertyValueFactory
's constructor is the name of the property that is used to compute the value to display, not the actual value to display.
So if you do
cID.setCellValueFactory(new PropertyValueFactory<Student, String>("studentPin"));
then for each (populated) row in the table, the table view will look at the Student
in that row and call getStudentPin()
to determine the value to be displayed.
For your populateStudentTable()
method, all you need is
@FMXL
public void populateStudentTable() {
studentTbl.setItems(studentObservableList);
cID.setCellValueFactory(new PropertyValueFactory<Student, String>("studentPin"));
}
The second line (cID.setCellValueFactory(...)
) should really go in the initialize()
method (or you can even do that directly in FXML).
Your students
list looks redundant: you can just use the studentObservableList
if you need to access the data.
Upvotes: 1