如果滚动到底部太快,JavaFX TableView 会崩溃,而且自定义单元格在操作上变得不合逻辑

JavaFX TableView crashes if scroll to bottom too fast, also custom cell becomes illogical on actions

所以首先我尝试将单元格涂成红色,或者根据同一行的另一列的值将单元格标为红色。 例如,有一个 "Enrolled" 日期列和一个 "DeadlineToEnrollBy" 日期列。这是一个通用的例子。

所以如果截止日期是明天,单元格不会显示为红色,因为学生还有时间,但如果截止日期是昨天并且学生还没有注册,那么 "Enrolled" 日期单元格将显示为红色,表示立即关注该学生。我实际上能够做到这一点,但 table 有时会 "funky"。

如果我快速滚动到底部,我会收到错误消息。有时上下滚动时,红色单元格会在其他地方,或者列中的所有单元格都会是红色,即使它不应该是红色。我相信即使上传新数据,因此刷新 table 也会导致红细胞关闭。

我得到的错误是

 Exception in thread "JavaFX Application Thread"
 java.lang.IndexOutOfBoundsException

在这行代码上

Person student= ClassPanelView.retrieveTable().getItems.get(getIndex());

这是相关的代码流

在类面板视图中

createTable(){
    .
    .
    TableColumn<Person, Date> enrolledBy = new TableColumn<>("Enrolled");
    enrolledBy.setCellValueFactory(new PropertyValueFactory<>("dateEnrolled"));
    enrolledBy.setCellFactory(column -> {
        return new util.EditEnrolledDateCell<Person, Date>();
    });
    table.getColumns().addAll(enrolledBy, ..etc);
}

public static TableView<Person> retrieveTable() {
    return table;
}

其他class/Cell栏目calls/returns

public class EditEnrolledDateCell<S,T> extends TextFieldTableCell<Person, Date> {
    private Date now = new Date();
...
...
        @Override
    public void updateItem(Date item, boolean empty) {
      super.updateItem(item, empty);

      if (item == null || empty) {
          if(this.getIndex() > -1) {
            /*int currentIndex = indexProperty().getValue() < 0 ? 0
                    : indexProperty().getValue(); */          

            Person student = ClassPanelView.retrieveTable().getItems().get(getIndex()); //<==== This line is the problem
            if(student.getDeadline != null && student.getDeadline.before(now)) {
                setStyle("-fx-border-color: #f40404;\n"
                        + "-fx-border-width: 1 1 1 1;\n");
            }
          } 
        }
        else { //if there is something here, format it
          setStyle("");
          setText(GuiUtils.monthFirstDateFormat.format(item));
        }
    }

}

任何 tips/knowledge 将不胜感激!谢谢

编辑:

你永远不会为没有超过截止日期的人重置单元格的样式。您需要进行此类更新。此外,table 末尾可能存在具有非负索引的空单元格。 (仅当单元格为空时才尝试检索此人。)

此外,为了使单元格更具可重用性,我建议使用 TableCell.getTableView 而不是访问 static 字段。

@Override
public void updateItem(Date item, boolean empty) {
    super.updateItem(item, empty);

    if (empty) {
        setStyle(null);
        setText("");
    } else {
        Person student = getTableView().getItems().get(getIndex());
        setStyle(student.getDeadline != null && !now.after(student.getDeadline)
                         ? null
                         : "-fx-border-color: #f40404; -fx-border-width: 1 1 1 1;");
        // I recommend passing the formatter in a constructor to make the cell type easier to reuse
        setText(item == null ? "" : GuiUtils.monthFirstDateFormat.format(item));
    }
}

注意:您还应该将字段 getDeadline 重命名为 deadlinegetDeadline 按照惯例是 deadline 属性 的 getter 方法的名称,我想不出以您通常使用的方式命名字段的原因getter 是个好习惯。

此外,我目前没有看到将类型参数添加到您的自定义单元格类型的理由。这些类型从未使用过。

public class EditEnrolledDateCell extends TextFieldTableCell<Person, Date>

在某些情况下,您可能想要保留它们。如果要使用带有 TableView<T> 的单元格,其中 TPerson 的子类型,或者在列中使用属于 Date 子类型的值类型,但是这需要不同的声明:

public class EditEnrolledDateCell<S extends Person, T extends Date> extends TextFieldTableCell<S, T> {

     ...

    @Override
    public void updateItem(T item, boolean empty) {
...

此外,如果可能,我建议使用 LocalDate 而不是 Date,因为这种类型更 modern/easier 使用。