【问题标题】:Adding Image and background to NatTable rows将图像和背景添加到 NatTable 行
【发布时间】:2017-03-25 01:03:32
【问题描述】:

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

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

    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);

【问题讨论】:

  • 问题是什么?你想摆脱配置中的单个 CellPainterWrapper 吗?我认为这不会以简单的方式起作用。或者上面的sn -p是你想做的吗?乍一看应该可以。您是否将两个标签都添加到标签累加器中的标签堆栈中?
  • 我的问题是如何使用上面的代码 sn-p 完成这项工作?我不想将我的图像和背景颜色代码混合到一个庞大的方法中。上面的 sn-p 是我想做的。每个 IConfigLabelAccumulator 都将自己的标签添加到堆栈中。换句话说,背景颜色标签累加器只处理背景颜色标签。图片标签累加器只处理图片标签。

标签: java nattable


【解决方案1】:

来自 cmets 的真正问题是关于如何支持分离的IConfigLabelAccumulator。由于每层只能注册一个IConfigLabelAccumulator,有两种方法可以实现:

  1. 在不同的层注册不同的IConfigLabelAccumulator
  2. 使用AggregateConfigLabelAccumulator 可以组合多个IConfigLabelAccumulator

入门教程中也对此进行了说明: http://www.vogella.com/tutorials/NatTable/article.html

【讨论】:

  • 太棒了,AggregateConfigLabelAccumulator 工作得很好!我更新了原始问题,以便其他人可以看到代码 sn-p
猜你喜欢
  • 2015-11-06
  • 2012-04-09
  • 2011-06-01
  • 2017-01-14
  • 2013-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多