【发布时间】:2022-01-01 19:51:14
【问题描述】:
我有一个TableView<MonthRow>,我想在其中显示一年中的每个月及其相关日期。
我得到了这三个类用于演示目的:
public record DayCell(
LocalDate date,
DayType type
) {
public String nameOfDay() {
return DateTimeFormatter.ofPattern("EE", Locale.ENGLISH).format(date);
}
}
public enum DayType {
COMPASSIONATE_LEAVE("C"),
EMPTY("E"),
NOT_ON_PAYROLL("N"),
QUARANTINE("Q"),
REGULAR_LEAVE("R"),
SICK_LEAVE("S"),
TRAINING("TRG"),
TRAVEL("T"),
UNPAID_LEAVE("U");
private final String shortName;
DayType(String shortName) {
this.shortName = shortName;
}
}
public record MonthRow(
String name,
DayCell[] days // amount of days in that specific month
) {
}
然后我创建表格内容:
public ObservableList<MonthRow> createMonth(int year) {
MonthRow[] months = new MonthRow[12];
for (int i = 0; i < months.length; i++) {
LocalDate date = LocalDate.of(year, i + 1, 1);
months[i] = new MonthRow(date.getMonth().getDisplayName(TextStyle.FULL, Locale.ENGLISH), createCells(date));
}
return FXCollections.observableArrayList(months);
}
public DayCell[] createCells(LocalDate date) {
DayCell[] cells = new DayCell[date.lengthOfMonth()];
for (int i = 0; i < cells.length; i++) {
cells[i] = new DayCell(date.plusDays(i), DayType.EMPTY);
}
return cells;
}
现在有了 TableView 的 ObservableList,我有点卡住了。我想要一个月的 TableColumn 和 DayCells 的 31 TableColumns。但是,因为它是一个数组,所以我不确定如何将每个 DayCell 的 cellData 反映到每个 TableCell 中。 此外,由于有些月份没有 31 天,因此, 单元格不应显示缺失日期的任何内容。
每个单元格都应根据DayType 显示nameOfDay() 内容和颜色(将在某个时候在相应的cellFactory 中完成)。
这可能是一个完全错误的方法,所以请不要犹豫,引导我找到不同的解决方案。
【问题讨论】: