如何将自定义样式设置为nattable中的某些列?

时间:2015-03-01 21:44:41

标签: java eclipse-rcp nattable

我需要设置一个不是所有人的自定义样式,而是设置nattable中的某些列。我不能像这样设置配置:

natTable.addConfiguration(new DefaultNatTableStyleConfiguration());

因为这会将配置设置为整个表。我必须仅将配置覆盖到特定列。在我的情况下,列应该具有如下水平对齐集:

setHAlign(HorizontalAlignmentEnum.RIGHT);

我怎样才能做到这一点? 谢谢!

1 个答案:

答案 0 :(得分:2)

来自NatTable styling docs

  

要启用条件样式,需要在IConfigRegistry中根据之前定义的标签注册自定义样式。

Style style = new Style();
// You can set other attributes here 
style.setAttributeValue(CellStyleAttributes.BACKGROUND_COLOR, GUIHelper.COLOR_RED);

configRegistry.registerConfigAttribute(
    CellConfigAttributes.CELL_STYLE,    // attribute to apply
    style,                  // value of the attribute
    DisplayMode.NORMAL,         // apply during normal rendering
    CELL_LABEL);    

        // apply for all cells with this label

要将CELL_LABEL标签应用于您的列,请按照NatTable configuration docs

中的说明操作
  

将标签附加到单元格

     

遵循整体设计惯例,图层可以添加标签   细胞。为了将标签附加到您需要实现的单元格   IConfigLabelAccumulator接口。该   每个都调用IConfigLabelAccumulator.accumulateConfigLabels()   层。每个图层都可以将其标签添加到LabelStack。

     

最常见的用例是开箱即用的,包括但是   不限于:

     

CellOverrideLabelAccumulator - 将标签应用于包含a的单元格   指定的数据值ColumnOverrideLabelAccumulator - 应用标签   列中的所有单元格您可以为您的自定义实现   自己的规则