如何在NatTable单元格中插入两个文本(每个文本使用不同的字体)和一个图像?并设置边距?

时间:2017-01-22 21:16:33

标签: java html css nattable

两个问题:

1)如何在NatTable单元格中插入两个文本(每个文本有不同的字体)和图像?

2)如何将边距设置为下图?

在以下情况中:

a)使用java 1.6(没有RichTexCellEditor,没有css) b)使用java 1.7(使用RichTexCellEditor,没有css) c)使用java 1.8(带有RichTexCellEditor,带有css)

Image example

非常感谢你。

1 个答案:

答案 0 :(得分:0)

  1. NatTable造型基于[1]中所述的风格和画家。有关NatTable配置和NatTable入门的更多信息,请查看我们的入门教程[2]
  2. 要创建与被询问者相似的组合,需要注册ICellPainter的组合。
  3. 要在单元格边框和内容之间添加填充(不是边距),请使用PaddingDecorator
  4. 要添加文字以外的图片,请使用CellPainterDecorator,其中包含一个画家作为基础,ImagePainter作为装饰。
  5. 仅使用NatTable星云扩展的RichTextCellPainter支持使用不同字体呈现文本。那个需要Java 1.7。
  6. NatTable中的CSS样式用于通过CSS创建样式配置。尽管如此,没有额外的支持。 CSS支持包含在需要Java 1.8的NatTable E4扩展中。有关解释,请参阅1.4 New&值得注意的页面[3]
  7. 所以a)的答案是:NatTable Core不支持使用两种不同字体呈现文本,因此您需要实现一个支持它的自定义ICellPainter

    b)的答案是创建一个复杂的画家并将其注册为:

    configRegistry.registerConfigAttribute(
        CellConfigAttributes.CELL_PAINTER,
        new BackgroundPainter(
                new PaddingDecorator(
                        new CellPainterDecorator(
                                new PaddingDecorator(new RichTextCellPainter(), 10, 0, 0, 0), 
                                CellEdgeEnum.LEFT, 
                                new ImagePainter()),
                        2, 5, 2, 5)),
        DisplayMode.NORMAL,
        ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 0);
    

    上述示例暗示ColumnLabelAccumulator应用了列标签,并且只应为第一列配置画家。另请注意,该具体单元格的样式需要设置属性CellStyleAttributes.IMAGE,然后由ImagePainter呈现。如果图像应该是固定的,则需要将其设置为构造函数参数。

    当然,内容需要包含用于呈现不同字体的必要HTML标记。类似的东西:

    <p><strong><span style="font-size:14px">This is a test</span></strong></p>
    <p><span style="color:rgb(128, 128, 128)"><span style="font-size:12px">This is a test</span></span></p>
    

    c)的答案并非微不足道。首先,上面显示的复杂画家结构不能用CSS完成。原因是需要配置多个填充,这是NatTable CSS不支持的。 其次,E4扩展不知道RichTextPainter。因此,需要手动注册到CellPainterFactory,例如

    CellPainterFactory.getInstance().registerContentPainter(
        "richtext",
        (painterProperties, underlying) -> {
            return new RichTextCellPainter();
        });
    

    需要以某种方式考虑处理其他属性。

    在CSS中它会看起来像这样:

    painter: background padding decorator richtext;
    decoration: left url('./nebula_logo_16.png') 5 true;
    padding: 2 5;
    

    [1] https://www.eclipse.org/nattable/documentation.php?page=styling
    [2] http://www.vogella.com/tutorials/NatTable/article.html
    [3] https://www.eclipse.org/nattable/nandn/nandn_140.php