突出显示 JavaFX TableRow 中的多个单元格

Highlighting multiple cells in a JavaFX TableRow

Jewelsea 提供了一个很好的示例,可以突出显示 table 行和 GitHub 处的单个单元格。但是,我对一些密切相关的事情感到非常困难。

这是一个屏幕截图:

使用他的示例,当我对 "Will Pay Up" 列中的单个单元格执行 updateItem 代码时,我还想 突出显示相应的名称 (这将在同一个 TableRow 中找到),其颜色与 will pay up 列中的单元格相同。在我实际开发的代码中,我确保名称始终出现在第一列中。

以下是他的 updateItem 代码中的几行(希望没问题):

// update the item and set a custom style if necessary
        if (item != null) {
          setText(item.toString());
          this.getStyleClass().add(item ? "willPayCell" : "wontPayCell");
          this.getTableRow().getStyleClass().add(item ? "willPayRow" : "wontPayRow");
        }

我们可以突出显示当前单元格 (this.getStyleClass()) 或整行 (this.getTableRow()),但我无法找到访问当前行中另一个单元格的方法.

将单元格的样式 class 设置为常量,并根据项目的值将行的样式 class 设置为:

if (item != null) {
    setText(item.toString());
    if (! this.getStyleClass().contains("willPayCell")) {
        this.getStyleClass().add("willPayCell");
    }
    this.getTableRow().getStyleClass().removeAll("willPayRow", "wontPayRow");
    this.getTableRow().getStyleClass().add(item ? "willPayRow" : "wontPayRow");
}

然后你可以使用CSS来确定不同单元格的颜色:

.table-row-cell.willPayRow .table-cell {
    -fx-background: /* color for cells other than willPayCell in willPayRow */;
}
.table-row-cell.wontPayRow .table-cell {
    -fx-background: /* color for cells other than willPayCell in wontPayRow */;
}
.table-row-cell.willPayRow .willPayCell {
    -fx-background: /* color for willPayCell in willPayRow */;
}
.table-row-cell.wontPayRow .willPayCell {
    -fx-background: /* color for willPayCell in wontPayRow */;
}