将图像和背景添加到NatTable行

时间:2016-11-10 21:16:44

标签: java nattable

我已阅读以下文档:https://eclipse.org/nattable/documentation.php?page=styling

我很好奇是否有任何简单的方法可以使用单独的配置添加背景行颜色和图像。我不希望将它们组合成1个配置,如CellPainterWrapper示例,因为我想将两者之间的逻辑分开。我当前的代码适用于图像或背景颜色,但我不能同时执行这两种操作(最顶层的配置会覆盖最底层的配置)。以下是我的代码:

    void run(){
           addBackgroundRowColors();
           addImageToColumn();
    }

    void addImageToColumn() {
        getNatTable().addConfiguration(new AbstractRegistryConfiguration() {
            @Override
            public void configureRegistry(IConfigRegistry configRegistry) {
                final Style cellStyleOne = new Style();
                cellStyleOne.setAttributeValue(CellStyleAttributes.IMAGE,
                   myIcon);
                configRegistry.registerConfigAttribute(
                   CellConfigAttributes.CELL_STYLE, cellStyleOne,
                   DisplayMode.NORMAL, myIconLabel);

                final Style cellStyleTwo = new Style();
                cellStyleTwo.setAttributeValue(CellStyleAttributes.IMAGE, 
                   myIcon2);
                configRegistry.registerConfigAttribute(
                   CellConfigAttributes.CELL_STYLE, cellStyleTwo,
                   DisplayMode.NORMAL, myIconLabel2);

                configRegistry.registerConfigAttribute(
                   CellConfigAttributes.CELL_PAINTER,
                   new CellPainterDecorator(new TextPainter(), 
                   CellEdgeEnum.LEFT, 10, new ImagePainter()),
                   DisplayMode.NORMAL);
            }
        });
        DataLayer dl = getGlazedListsGridLayer().getBodyDataLayer();
        IConfigLabelAccumulator cellLabelAccumulator = (configLabels, 
          columnPosition, rowPosition) -> {
               // Label code here...
        };

        dl.setConfigLabelAccumulator(cellLabelAccumulator);
    }

    void addBackgroundRowColors() {
        getNatTable().addConfiguration(new AbstractRegistryConfiguration() {
            @Override
            public void configureRegistry(IConfigRegistry configRegistry) {
                Style cellStyleOne = new Style();
                cellStyleOne.setAttributeValue(
                  CellStyleAttributes.BACKGROUND_COLOR, myColorOne);
                configRegistry.registerConfigAttribute(
                  CellConfigAttributes.CELL_STYLE, cellStyleOne,
                  DisplayMode.NORMAL, myColorLabel1);

                Style cellStyleTwo = new Style();
                cellStyleTwo.setAttributeValue(
                  CellStyleAttributes.BACKGROUND_COLOR, cellStyleTwo);
                configRegistry.registerConfigAttribute(
                  CellConfigAttributes.CELL_STYLE, cellStyleTwo,
                  DisplayMode.NORMAL, myColorLabel2);
            }
        });
        DataLayer dl = getGlazedListsGridLayer().getBodyDataLayer();
        IConfigLabelAccumulator cellLabelAccumulator = (configLabels, 
          columnPosition, rowPosition) -> {
               // Label code here...
        };

       dl.setConfigLabelAccumulator(cellLabelAccumulator);
    }

更新

我最终做了类似以下的事情以使其发挥作用:

  AggregateConfigLabelAccumulator aggregate = 
    new AggregateConfigLabelAccumulator();
  aggregate.add(addImageToColumn());
  aggregate.add(addBackgroundRowColors());

  getGlazedListsGridLayer().getBodyDataLayer().
    setConfigLabelAccumulator(aggregate);

1 个答案:

答案 0 :(得分:2)

从评论中,真正的问题是如何支持分离的IConfigLabelAccumulator。由于每层只能注册一个IConfigLabelAccumulator,因此有两种方法可以实现此目的:

  1. 在不同的图层上注册不同的IConfigLabelAccumulator
  2. 使用AggregateConfigLabelAccumulator您可以合并多个IConfigLabelAccumulator
  3. “入门教程”中也对此进行了解释: http://www.vogella.com/tutorials/NatTable/article.html