【问题标题】:Dynamically change row foreground color of particular cell in NatTable动态更改 NatTable 中特定单元格的行前景色
【发布时间】:2018-09-15 12:48:10
【问题描述】:

您是否有动态更改单元格颜色的建议,我在下面尝试过,但是当表格没有这些标签(“FAILURE”)时,即使该行仍保持彩色。当表格中不存在 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);

            }
        });
    }

【问题讨论】:

    标签: java nattable


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2020-04-18
      • 2013-08-20
      • 2017-03-09
      • 1970-01-01
      • 2014-02-10
      • 2021-03-10
      • 2012-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多