动态更改 NatTable 中特定单元格的行前景色

Dynamically change row foreground color of particular cell in NatTable

您有动态更改单元格颜色的建议吗,我在下面尝试过,但是当 table 没有那些标签 ("FAILURE") 时,即使在该行之后仍将保持彩色。当 table.

中不存在 FAILURE 标签时,我想恢复彩色单元格的颜色
private static final String FAILURE = "FAILURE";
void example(){
final DefaultBodyLayerStack bodyLayer = underlyingLayer.getBodyLayer();



        // Custom label "FAILURE" for cell
        IConfigLabelAccumulator cellLabelAccumulator = new IConfigLabelAccumulator() {
            Integer rowCount = null;

            @Override
            public void accumulateConfigLabels(LabelStack configLabels,
                    int columnPosition, int rowPosition) {
                int rowIndex = bodyLayer.getRowIndexByPosition(rowPosition);

                for (GridConsoleRow gridConsoleRow : underlyingLayer.getBodyDataProvider().getList()) {
                    if (StringUtils.equals(gridConsoleRow.getLogLevel().trim(), FAILURE)) {
                            rowCount = bodyLayer.getPreferredRowCount()-1;
                            break;
                        }
                    }

                    if (rowCount != null && rowIndex == rowCount.intValue()) {
                    configLabels.addLabel(FAILURE);
                    }
                }
            };
        bodyLayer.setConfigLabelAccumulator(cellLabelAccumulator);

        // Custom style for label "FAILURE"
        natTable.addConfiguration(new AbstractRegistryConfiguration() {
            @Override
            public void configureRegistry(IConfigRegistry configRegistry) {
                Style cellStyle = new Style();
                cellStyle.setAttributeValue(
                        CellStyleAttributes.FOREGROUND_COLOR,
                        GUIHelper.COLOR_RED);
                configRegistry.registerConfigAttribute(
                        CellConfigAttributes.CELL_STYLE, cellStyle,
                        DisplayMode.NORMAL, FAILURE);

            }
        });
    }

你调试过真的没有FAILURE标签吗? NatTable 只显示它应该显示的内容,因此如果您仅为 FAILURE 标签注册了带有红色背景的样式,它只会将具有该标签的行呈现为红色。老实说,我不明白你IConfigLabelAccumulator的逻辑。可能 rowCount 成员是原因,因为如果没有失败,您再也不会将其设置回 null 。不确定为什么要将该信息存储在成员变量中。