【问题标题】:Highlight text in nattable cell突出显示可导航单元格中的文本
【发布时间】:2021-09-01 10:39:44
【问题描述】:

我有一个带有不同列和行的网表,其中填充了数据。当在文本字段中搜索某些文本时,我想突出显示单元格中的特定文本。

查看https://www.eclipse.org/nattable/nandn/nandn_150.php后 我尝试了以下操作,但按下应用按钮时没有突出显示

this.applyButton = new Button(parent, SWT.PUSH);
this.applyButton.addSelectionListener(getSelectionListener());

private SelectionListener getSelectionListener(){
    return new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            String text = textBox.getText();
            if(!text.trim().isEmpty()) {
                applyHighlighting(text);
            }
        }
    };
}

public void applyHighlighting(String text) {
    RegexMarkupValue regexMarkup = new RegexMarkupValue("", "<span style=\"background-color:rgb(255, 255, 0)\">",
            "</span>");
    natTable.addConfiguration(new DefaultNatTableStyleConfiguration() {
        {
            this.cellPainter = new BackgroundPainter(new PaddingDecorator(new RichTextCellPainter(), 2));
        }

        @Override
        public void configureRegistry(IConfigRegistry configRegistry) {
            super.configureRegistry(configRegistry);
            // markup for highlighting
            MarkupDisplayConverter markupConverter = new MarkupDisplayConverter();
            markupConverter.registerMarkup("highlight", regexMarkup);
            // register markup display converter for normal display mode in the body
            configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, markupConverter,
                    DisplayMode.NORMAL, GridRegion.BODY);
        }
    });
    regexMarkup.setRegexValue(text.isEmpty() ? "" : "(" + text + ")");
}

因此,我假设当我在文本字段中键入内容并单击文本字段中提供的单元格中的应用按钮时,文本应突出显示,但没有任何更改。有什么帮助吗?

谢谢

【问题讨论】:

    标签: java eclipse eclipse-rcp nattable


    【解决方案1】:

    如果您只想突出显示单元格内的部分文本,则需要 Nebula Extension。在 1.5 的 New & Noteworthy 中有描述

    https://www.eclipse.org/nattable/nandn/nandn_150.php

    还有一个指向对应例子的指针。

    【讨论】:

    • 是否可以在不使用扩展名的情况下突出显示单元格中的文本?
    • 只有当你实现一个可以以这种方式呈现文本的画家时。 NatTable 仅通过 RichTextCellPainter 提供此功能
    • 能否请您查看编辑后的代码并帮助我解决我做错了什么
    • 代码似乎正确。您是否有一个网格组合,其网格区域具有区域标签 BODY?
    • 是的,我有 viewportLayer.setRegionName(GridRegion.BODY);
    猜你喜欢
    • 1970-01-01
    • 2011-03-27
    • 2014-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多