我在javafx中有一个tableview,我想填充对象类型Course。我的想法是,在我的课程课程中,我有一个复合主键,它是一个不同的类CourseId。我想在tableview的一列中添加一个在CourseId类中存在的courseno,但我不知道如何获得它。
我的课程课程:
package com.licenta.ascourses.ui.model;
import java.io.Serializable;
public class CourseId implements Serializable {
private int idDiscipline;
private int idProfessor;
private int courseNo;
public CourseId() {
}
public CourseId(int idDiscipline, int idProfessor, int courseNo) {
super();
this.idDiscipline = idDiscipline;
this.idProfessor = idProfessor;
this.courseNo = courseNo;
}
public int getIdDiscipline() {
return idDiscipline;
}
public void setIdDiscipline(int idDiscipline) {
this.idDiscipline = idDiscipline;
}
public int getIdProfessor() {
return idProfessor;
}
public void setIdProfessor(int idProfessor) {
this.idProfessor = idProfessor;
}
public int getCourseNo() {
return courseNo;
}
public void setCourseNo(int courseNo) {
this.courseNo = courseNo;
}
public boolean equals(Object o) {
return true;
}
public int hashCode() {
return 1;
}
}
columnNumarCurs.setCellValueFactory(new PropertyValueFactory<Course, Integer>(""));
columnAn.setCellValueFactory(new PropertyValueFactory<Course, Integer>("year"));
columnSemestru.setCellValueFactory(new PropertyValueFactory<Course, Integer>("semester"));
columnDisciplina.setCellValueFactory(new PropertyValueFactory<Course, String>("discipline"));
columnProfesor.setCellValueFactory(new PropertyValueFactory<Course, String>("professor"));
我的课程上课:
UINavigationController
答案 0 :(得分:1)
setCellValueFactory
方法需要Callback<CellDataFeatures, ObservableValue>
:即将CellDataFeatures
对象映射到包含要显示的值的ObservableValue
的函数。由于您拥有的值为int
,并且假设columnNumarCurs
为TableColumn<Course, Number>
,因此相应的ObservableValue
类型为IntegerProperty
。所以你可以这样做:
columnNumarCurs.setCellValueFactory(
cellData -> new SimpleIntegerProperty(cellData.getValue().getIdCourse().getCourseNo()));