为 JTable 单元格中的文本着色

Coloring Text in JTable Cell

我想为 JTable 单元格中的文本着色。我正在使用带有 HTML 标签的 DefaultTableCellRender 来为多重 words/text 着色。我正在使用 Regex 查找 words/text 并通过添加 HTML 标签来替换它们。

这里的问题是 HTML 标签本身不应该与正则表达式匹配。

示例:

正文:

This is a example text background

文本颜色 "a example":

This is <font color="FFFFFF" style="background-color: #FFAABB">a example</font> 
text background

下一个颜色词 "back":

This is <font color="FFFFFF" style="background-color: #FFAABB">a example</font> 
text <font color="FFFFFF" style="background-color: #AAAAAA">back</font>ground

HTML标签中的"back"不应该被替换。有没有办法通过正则表达式排除它?

代码:

private String color(String val, ArrayList<ColorKeyWord> list) {
        for(ColorKeyWord ckw: list){
            val = val.replaceAll(ckw.getKeyWord(), "<font color=\"" + DecodedDataHTMLTags.color_white + "\" " +"style=\"background-color: #" + ckw.getColor() + "\">" + ckw.getKeyWord() + "</font>");
        }
        return val;
    }

我认为一个更简单的解决方案是给我们 StyledLabel 从 jidesoft 并使用 StyleRange。但是 StyledTableCellRenderer 不包含在免费库中。 我也在使用 JTable 因为我需要可变的单元格高度。这无法通过 swt 表实现。

试试看 设置颜色(黑色); 在对象上。

您需要使用更复杂的正则表达式模式进行替换(并忽略 html 标签内的关键字)。 'quick and dirty' 解决方案:

private static String color(String val, String keyword) {
    String pattern = "[^>].*(" + keyword + ").*[^<]";

    Pattern r = Pattern.compile(pattern);
    Matcher m = r.matcher(val);
    if (m.find()) {
        int startIndex = m.start(1);
        int endIndex = m.end(1);
        String withReplacedKeyword = val.substring(0, startIndex)
                + "<font color=\"FFFFFF\" style=\"background-color: #FFAABB\">"
                + keyword + "</font>" + val.substring(endIndex);
        return withReplacedKeyword;
    }       
    return val;

}

public static void main(String[] args) {
    System.out.println(color("This is a example text background", "back"));
    System.out.println(color("This is a example text <font color=\"FFFFFF\" style=\"background-color: #FFAABB\">back</font>ground", "back"));
    System.out.println(color("This is a example text <font color=\"FFFFFF\" style=\"background-color: #FFAABB\">back</font>ground", "is")); 
}

因此第一次替换:

This is a example text 
<font color="FFFFFF" style="background-color: #FFAABB">
    back
</font>
ground

第二个看起来像嵌套标签(不会破坏 GUI 组件):

This is a example text 
<font color="FFFFFF" style="background-color: #FFAABB">
    <font color="FFFFFF" style="background-color: #FFAABB">
        back
    </font>
</font>
ground

唯一的缺点是某些特殊情况(例如在原始字符串的开头进行替换)可能需要额外的逻辑。为所有这些情况编写通用正则表达式模式可能很费力。

可能有更好的方法,但基本上,它的作用是设置一系列可选组,允许 PatternMatcherString 分解为 "ranges"

然后我们使用这些范围来 "inject" 规则...

String text = "This is a example text background and bunnies are red";
Pattern p = Pattern.compile("(example text)|(back)|(bunnies)|(red)", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(text);

List<MatchRange> ranges = new ArrayList<>(25);
while (m.find()) {
    ranges.add(new MatchRange(m.start(), m.end()));
}

StringBuilder sb = new StringBuilder(64);
sb.append("<html>");
int anchor = 0;
for (MatchRange range : ranges) {
    String before = "";
    if (anchor < range.getStart()) {
        before = text.substring(anchor, range.getStart());
    }
    sb.append(before);
    System.out.println(range.getStart() + " - " + range.getEnd());
    String match = text.substring(range.getStart(), range.getEnd());
    // This is where I would have a rule formatter
    if (match.equals("example text")) {
        sb.append("<font color=\"FFFFFF\" style=\"background-color: #FFAABB\">");
        sb.append(match);
        sb.append("</font>");
    } else if (match.equals("back")) {
        sb.append("<font color=\"FFFFFF\" style=\"background-color: #AAAAAA\">");
        sb.append(match);
        sb.append("</font>");
    } else if (match.equals("bunnies")) {
        sb.append("<font color=\"FF0000\" style=\"background-color: #FFFFFF\">");
        sb.append(match);
        sb.append("</font>");
    } else if (match.equals("red")) {
        sb.append("<font color=\"FF0000\" style=\"background-color: #000000\">");
        sb.append(match);
        sb.append("</font>");
    } else {
        sb.append(match);
    }
    anchor = range.getEnd();
}

System.out.println(sb.toString());

还有 MatchRange...

public class MatchRange {
    private final int start;
    private final int end;

    public MatchRange(int start, int end) {
        this.start = start;
        this.end = end;
    }

    public int getEnd() {
        return end;
    }

    public int getStart() {
        return start;
    }

}

这基本上输出

<html>This is a <font color="FFFFFF" style="background-color: #FFAABB">example text</font> <font color="FFFFFF" style="background-color: #AAAAAA">back</font>ground and <font color="FF0000" style="background-color: #FFFFFF">bunnies</font> are <font color="FF0000" style="background-color: #000000">red</font>

我添加了一些额外的测试条件。

我会做的是创建一个 class 可以携带条件 ("example text") 并且可以格式化值(例如将 HTML 包裹起来)和简单地迭代这些来创建表达式并应用格式

也许像...

public interface ConditionFormatter {
    public String getCondition();
    public String applyFormatTo(String text);
    public boolean matches(String text);
}

public class DefaultConditionFormatter implements ConditionFormatter {

    private final String condition;
    private final String preFormat;
    private final String postFormat;

    public DefaultConditionFormatter(String condition, String preFormat, String postFormat) {
        this.condition = condition;
        this.preFormat = preFormat;
        this.postFormat = postFormat;
    }

    @Override
    public String getCondition() {
        return condition;
    }

    @Override
    public String applyFormatTo(String text) {
        return new StringBuilder(preFormat).append(text).append(postFormat).toString();
    }

    @Override
    public boolean matches(String text) {
        return condition.equalsIgnoreCase(text);
    }

}

其中包含 "condition" 或 "rule" 以及要应用的格式。通常,我可能会想将 "rule" 和 "formatter" 分开,但我认为您可以了解基本概念...

然后你可以做类似...

List<ConditionFormatter> formatters = new ArrayList<>(25);
formatters.add(new DefaultConditionFormatter("example text", "<font color=\"FFFFFF\" style=\"background-color: #FFAABB\">", "</font>"));
formatters.add(new DefaultConditionFormatter("back", "<font color=\"FFFFFF\" style=\"background-color: #AAAAAA\">", "</font>"));
formatters.add(new DefaultConditionFormatter("bunnies", "<font color=\"FF0000\" style=\"background-color: #FFFFFF\">", "</font>"));
formatters.add(new DefaultConditionFormatter("red", "<font color=\"FF0000\" style=\"background-color: #000000\">", "</font>"));

String text = "This is a example text background and bunnies are red";
StringJoiner sj = new StringJoiner(")|(", "(", ")");
for (ConditionFormatter formatter : formatters) {
    sj.add(formatter.getCondition());
}

Pattern p = Pattern.compile(sj.toString(), Pattern.CASE_INSENSITIVE);
//...

还有...

for (MatchRange range : ranges) {
    //...
    // This is where I would have a rule formatter
    String match = text.substring(range.getStart(), range.getEnd());
    for (ConditionFormatter formatter : formatters) {
        if (formatter.matches(match)) {
            sb.append(formatter.applyFormatTo(match));
            break;
        }
    }
    //...
}