如何在两行之间添加n个自定义单元格

时间:2019-01-10 03:46:54

标签: uitableview javafx cell multiple-columns javafx-tableview

所以我试图将课程表可视化。它看起来应该像这样: 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;
  } 
}

使用此代码,我将我的Lesson对象转换为ClassHour对象,因为我使用ClassHour对象将课程保存在表中:

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;
}

最后一步是在表中显示数据:

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个单元格/行。 current table

1 个答案:

答案 0 :(得分:0)

请注意,当且仅当有一个convertToClassHourClassHour不是按小时排序时,Lesson才会创建一个Lesson实例,{输出中的{1}}个错误。

除非您有预定的小时数,否则您需要找到最小和最大小时数来解决问题:

ClassHour