【问题标题】:Displaying images in NatTable cells在 NatTable 单元格中显示图像
【发布时间】:2016-04-19 21:22:51
【问题描述】:

我对在 NatTable 单元格中使用图像有疑问。我的 ConfigLabelAccumulator 和 Configuration 看起来像这样

public class MyConfigLabelAccumulator implements IConfigLabelAccumulator {
  @Override
  public void accumulateConfigLabels(final LabelStack configLabels, final int columnPosition, final int rowPosition) {
    if (((rowPosition + columnPosition) % 2) == 0) {
      configLabels.addLabel("myLabel");
    }
  }
}

public class MyStyleConfiguration extends DefaultNatTableStyleConfiguration {
  @Override
  public void configureRegistry(final IConfigRegistry configRegistry) {
    super.configureRegistry(configRegistry);
    final Style style = new Style();
    style.setAttributeValue(CellStyleAttributes.IMAGE, GUIHelper.getImage("plus"));
    style.setAttributeValue(CellStyleAttributes.BACKGROUND_COLOR, GUIHelper.COLOR_YELLOW);
    configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, style, DisplayMode.NORMAL, "myLabel");
    configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, new CellPainterDecorator(new TextPainter(), CellEdgeEnum.RIGHT, new ImagePainter()), DisplayMode.NORMAL);
  }
}   

我是这样配置的

dataLayer.setConfigLabelAccumulator(new MyConfigLabelAccumulator());
...
natTable.addConfiguration(new MyStyleConfiguration());
...
natTable.configure();   

表格看起来像预期的那样。我在单元格中看到黄色背景单元格和“+”图像。但通话后

natTable.setTheme(new ModernNatTableThemeConfiguration());

我只看到黄色背景,没有图像。

统一更新: 我已经使用IThemeExtension 解决了这个问题,但可能还有其他解决方案吗?

【问题讨论】:

  • 您是否检查过 ModernNatTableThemConfiguration 是否替换了您的配置 MyStyleConfiguration 的 configureRegistry?可能你想先设置主题然后添加你的配置 nattable.addConfiguration( new MyStyleConfiguration() );
  • @svasa 据我所知 setTheme() 应该在 configure() 之后调用,所以如果我在 setTheme() 之后调用 addConfiguration() 不会有任何效果
  • ModernNattablethemeconfiguration 实际上有自己的图像画家,如果你看它,那么如果你使用它进行配置,据我所知它将取代你以前的 mystyleconfiguration。或者自定义modernnattablethem配置。

标签: java swt nattable


【解决方案1】:

主题配置旨在覆盖现有样式。这也允许在运行时切换主题。

IThemeExtensions 是使用条件样式扩展现有主题的方法。当然,您也可以通过扩展现有主题来创建自己的主题,但这样您的自定义无法与其他主题一起使用。

上面代码中的问题似乎是您在注册画家,而不仅仅是为您的“myLabel”。这是故意的吗?因为这会覆盖默认的单元格绘制器配置,然后再被主题覆盖。如果只为“myLabel”设置注册,则主题不应该有效果。

【讨论】:

  • 再次感谢您!我确实错过了画家的“myLabel”
猜你喜欢
  • 2016-08-28
  • 1970-01-01
  • 2019-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多