【问题标题】:JavaFX TableView with array of TableColumns带有 TableColumns 数组的 JavaFX TableView
【发布时间】: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 中完成)。

TableView Example

这可能是一个完全错误的方法,所以请不要犹豫,引导我找到不同的解决方案。

【问题讨论】:

    标签: java javafx


    【解决方案1】:

    你可以这样做:

    TableView<MonthRow> table = new TableView<>();
    TableColumn<MonthRow, String> monthColumn = new TableColumn<>("Month");
    monthColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getName()));
    table.getColumns().add(monthColumn);
    
    for (int i = 0; i < 31 ; i++) {
        TableColumn<MonthRow, DayCell> dayColumn = new TableColumn<>(Integer.toString(i+1));
        final int index = i ;
        dayColumn.setCellValueFactory(cellData -> {
            MonthRow month = cellData.getValue();
            if (index < month.getDays().size()) {
                return new SimpleObjectProperty<>(month.getDays()[index]);
            } else {
                return new SimpleObjectProperty<>(null);
            }
        });
        dayColumn.setCellFactory(new TableCell<>() {
            @Override
            protected void updateItem(DayCell day, boolean empty) {
                super.updateItem(day, empty);
                if (empty | day == null) {
                    setText("");
                } else {
                    setText(day.nameOfDay());
                }
            }
        });
        table.getColumns().add(dayColumn);
    }
    

    这可能是一个完全错误的方法

    也许吧。您在这里需要TableView 的功能吗?例如。选择一个月(行)等?也许在ScrollPane 中使用简单的GridPane 会更方便;这真的取决于您的项目要求。

    【讨论】:

    • 干杯,正是我想要实现的目标。甚至自己完成了一半:D 是的,我也在考虑使用 GridPane,但我有点想先实现这个,看看它是否能令我满意。
    • 所以我设法让它看起来像link。而且因为我想组合/合并同一天的类型以使其看起来像一个日历,tableview 的想法似乎妨碍了我,因为我总是必须将表格单元格与我正在显示的节点相关联。我现在将尝试 GridPane 方法,看看。
    猜你喜欢
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    • 2018-08-12
    • 1970-01-01
    • 2020-06-30
    相关资源
    最近更新 更多