簡體   English   中英

動態更改NatTable中特定單元格的行前景色

[英]Dynamically change row foreground color of particular cell in NatTable

您是否有建議動態更改單元格的顏色,我在下面嘗試過,但是當表格沒有那些標簽(“ 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);

            }
        });
    }

您是否調試過FAILURE標簽確實不存在? NatTable僅顯示應該顯示的內容,因此,如果僅將FAILURE標簽的樣式注冊為紅色背景,則它將僅以紅色顯示具有該標簽的行。 老實說,我不了解您的IConfigLabelAccumulator的邏輯。 rowCount成員可能是原因,因為如果沒有失敗,則永遠不要將其重新設置為null 不知道為什么要以任何方式將這些信息存儲在成員變量中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM