gpt4 book ai didi

java - 在javafx中填充表格 View

转载 作者:行者123 更新时间:2023-12-02 13:42:47 24 4
gpt4 key购买 nike

我在 javafx 中有一个 TableView ,我想用对象类型 Course 填充它。这个想法是,在我的 Course 类中,我有一个复合主键,它是一个不同的类 CourseId。我想在 TableView 的一列中添加 CourseId 类中存在的 courseno,但我不知道如何获取它。

我的类(class)类(class):

package com.licenta.ascourses.ui.model;

import java.io.Serializable;

public class Course implements Serializable {

private CourseId idCourse = new CourseId();
private int year;
private int semester;
private String discipline;
private String professor;

public Course() {

}

public Course(CourseId idCourse, int year, int semester) {
super();
this.idCourse = idCourse;
this.year = year;
this.semester = semester;
}

public Course(CourseId idCourse, int year, int semester, String discipline, String professor) {
this.idCourse=idCourse;
this.year = year;
this.semester = semester;
this.discipline = discipline;
this.professor = professor;
}

public CourseId getIdCourse() {
return idCourse;
}

public void setIdCourse(CourseId idCourse) {
this.idCourse = idCourse;
}

public int getYear() {
return year;
}

public void setYear(int year) {
this.year = year;
}

public int getSemester() {
return semester;
}

public void setSemester(int semester) {
this.semester = semester;
}

public String getDiscipline() {
return discipline;
}

public void setDiscipline(String discipline) {
this.discipline = discipline;
}

public String getProfessor() {
return professor;
}

public void setProfessor(String professor) {
this.professor = professor;
}



}

我的 courseId 类(class):

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"));

最佳答案

setCellValueFactory 方法需要 Callback<CellDataFeatures, ObservableValue> :即映射 CellDataFeatures 的函数反对ObservableValue包含要显示的值。由于您拥有的值是 int ,并假设columnNumarCursTableColumn<Course, Number> ,适当的ObservableValue类型是 IntegerProperty 。所以你可以这样做:

columnNumarCurs.setCellValueFactory(
cellData -> new SimpleIntegerProperty(cellData.getValue().getIdCourse().getCourseNo()));

关于java - 在javafx中填充表格 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42644404/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com