如何在两行之间添加 n 个自定义单元格
How to add n- custom cells between two rows
所以我试图将课程可视化为 table。它应该是这样的:
如您所见,有些自定义单元格 (+) 不是课程。它们是按钮。
我有两个类:
public class Lesson {
private Room schoolRoom;
private Room teachingRoom;
private TeacherSpecialization teachingInfo;
private WeekDay weekDay;
private int schoolHour;
}
和
public class ClassHour {
Lesson[] dayLessons = new Lesson[18];
private int hour;
public ClassHour(int hour) {
this.hour = hour;
}
}
使用此代码我将课程对象转换为 ClassHour 对象,因为我使用 ClassHour 对象将课程保存在 table:
public ObservableList<ClassHour> convertToClassHour(List<Lesson> lessons) {
ObservableList<ClassHour> classHours = FXCollections.observableArrayList();
// Converting Lessons to ClassHour objects.
lessons.forEach(lesson -> {
ClassHour classHour = classHours.stream().filter(ch -> ch.getHour() == lesson.getSchoolHour()).findFirst().orElse(null);
if (classHour == null) {
classHour = new ClassHour(lesson.getSchoolHour());
classHours.add(classHour);
}
classHour.getDayLessons()[lesson.getWeekDay().ordinal()] = lesson;
});
return classHours;
}
最后一步是显示 table 中的数据:
private void showLessons(String roomNr) throws Exception {
try {
// lessons.addListener((ListChangeListener) e -> repopulate(lessons, classHours));
ArrayList<Lesson> allLessonsByRoomNr = db.getAllLessonsByRoomNr(roomNr);
ObservableList<ClassHour> classHours = db.convertToClassHour(allLessonsByRoomNr);
for (int i = 0; i < 5; i++) {
int day = i;
TableColumn<ClassHour, Lesson> dayColumn = new TableColumn<>(WeekDay.values()[i].name());
dayColumn.setSortable(false);
dayColumn.setCellValueFactory(param -> new SimpleObjectProperty(param.getValue().getDayLessons()[day]));
dayColumn.setCellFactory((TableColumn<ClassHour, Lesson> param) -> new TableCell<ClassHour, Lesson>() {
@Override
protected void updateItem(Lesson item, boolean empty) {
super.updateItem(item, empty);
setText(null);
setGraphic(null);
if (!empty) {
if (item != null) {
setText(item.toString());
} else {
Button btn = new Button("+ ADD");
btn.setOnAction(e -> {
tableLessons.getSelectionModel().select((ClassHour) getTableRow().getItem());
showAdd(day, ((ClassHour) getTableRow().getItem()).getHour(), btn);
});
setGraphic(new StackPane(btn));
}
}
}
});
tableLessons.getColumns().addAll(dayColumn);
}
tableLessons.setItems(classHours);
} catch (Exception ex) {
showResultDialog("An error has occured:", ex.getMessage());
}
}
我遇到的问题是自定义单元格(+ 按钮)。出于某种原因,我无法在两节课之间画出它们。在下面的案例中,第 10 课时和第 16 课时(星期一)之间应该有 5 cells/rows。
请注意,当且仅当存在 Lesson
且 Lesson
未按小时排序时,您的 convertToClassHour
会创建一个 ClassHour
实例,该顺序输出中的 ClassHour
是错误的。
除非您有预定的工作时间,否则您需要找到最短和最长的工作时间来解决您的问题:
public ObservableList<ClassHour> convertToClassHour(List<Lesson> lessons) {
ObservableList<ClassHour> classHours = FXCollections.observableArrayList();
if (!lessons.isEmpty()) {
// find required hour range
int minHour = lessons.mapToInt(Lesson::getSchoolHour).min().getAsInt();
int maxHour = lessons.mapToInt(Lesson::getSchoolHour).max().getAsInt();
// create ClassHours for range
for (int i = minHour; i <= maxHour; i++) {
classHours.add(new ClassHour(i));
}
// fill classHours with lessons
for (Lesson lesson : lessons) {
classHours.get(lesson.getSchoolHour() - minHour).getDayLessons()[lesson.getWeekDay().ordinal()] = lesson;
}
}
return classHours;
}
所以我试图将课程可视化为 table。它应该是这样的:
如您所见,有些自定义单元格 (+) 不是课程。它们是按钮。
我有两个类:
public class Lesson {
private Room schoolRoom;
private Room teachingRoom;
private TeacherSpecialization teachingInfo;
private WeekDay weekDay;
private int schoolHour;
}
和
public class ClassHour {
Lesson[] dayLessons = new Lesson[18];
private int hour;
public ClassHour(int hour) {
this.hour = hour;
}
}
使用此代码我将课程对象转换为 ClassHour 对象,因为我使用 ClassHour 对象将课程保存在 table:
public ObservableList<ClassHour> convertToClassHour(List<Lesson> lessons) {
ObservableList<ClassHour> classHours = FXCollections.observableArrayList();
// Converting Lessons to ClassHour objects.
lessons.forEach(lesson -> {
ClassHour classHour = classHours.stream().filter(ch -> ch.getHour() == lesson.getSchoolHour()).findFirst().orElse(null);
if (classHour == null) {
classHour = new ClassHour(lesson.getSchoolHour());
classHours.add(classHour);
}
classHour.getDayLessons()[lesson.getWeekDay().ordinal()] = lesson;
});
return classHours;
}
最后一步是显示 table 中的数据:
private void showLessons(String roomNr) throws Exception {
try {
// lessons.addListener((ListChangeListener) e -> repopulate(lessons, classHours));
ArrayList<Lesson> allLessonsByRoomNr = db.getAllLessonsByRoomNr(roomNr);
ObservableList<ClassHour> classHours = db.convertToClassHour(allLessonsByRoomNr);
for (int i = 0; i < 5; i++) {
int day = i;
TableColumn<ClassHour, Lesson> dayColumn = new TableColumn<>(WeekDay.values()[i].name());
dayColumn.setSortable(false);
dayColumn.setCellValueFactory(param -> new SimpleObjectProperty(param.getValue().getDayLessons()[day]));
dayColumn.setCellFactory((TableColumn<ClassHour, Lesson> param) -> new TableCell<ClassHour, Lesson>() {
@Override
protected void updateItem(Lesson item, boolean empty) {
super.updateItem(item, empty);
setText(null);
setGraphic(null);
if (!empty) {
if (item != null) {
setText(item.toString());
} else {
Button btn = new Button("+ ADD");
btn.setOnAction(e -> {
tableLessons.getSelectionModel().select((ClassHour) getTableRow().getItem());
showAdd(day, ((ClassHour) getTableRow().getItem()).getHour(), btn);
});
setGraphic(new StackPane(btn));
}
}
}
});
tableLessons.getColumns().addAll(dayColumn);
}
tableLessons.setItems(classHours);
} catch (Exception ex) {
showResultDialog("An error has occured:", ex.getMessage());
}
}
我遇到的问题是自定义单元格(+ 按钮)。出于某种原因,我无法在两节课之间画出它们。在下面的案例中,第 10 课时和第 16 课时(星期一)之间应该有 5 cells/rows。
请注意,当且仅当存在 Lesson
且 Lesson
未按小时排序时,您的 convertToClassHour
会创建一个 ClassHour
实例,该顺序输出中的 ClassHour
是错误的。
除非您有预定的工作时间,否则您需要找到最短和最长的工作时间来解决您的问题:
public ObservableList<ClassHour> convertToClassHour(List<Lesson> lessons) {
ObservableList<ClassHour> classHours = FXCollections.observableArrayList();
if (!lessons.isEmpty()) {
// find required hour range
int minHour = lessons.mapToInt(Lesson::getSchoolHour).min().getAsInt();
int maxHour = lessons.mapToInt(Lesson::getSchoolHour).max().getAsInt();
// create ClassHours for range
for (int i = minHour; i <= maxHour; i++) {
classHours.add(new ClassHour(i));
}
// fill classHours with lessons
for (Lesson lesson : lessons) {
classHours.get(lesson.getSchoolHour() - minHour).getDayLessons()[lesson.getWeekDay().ordinal()] = lesson;
}
}
return classHours;
}