JTable单元中的着色文本

时间:2015-06-15 09:46:06

标签: java regex swing jtable tablecellrenderer

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

这里的问题是它们自己的HTML标签不应该与正则表达式匹配。

示例:

文本:

This is a example text background

文字到颜色"一个例子":

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

下一个字的颜色&#34;返回&#34;:

This is <font color="FFFFFF" style="background-color: #FFAABB">a example</font> 
text <font color="FFFFFF" style="background-color: #AAAAAA">back</font>ground
&#34;返回&#34;不应替换HTML标记中的内容。有没有办法通过Regex排除这个?

代码:

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;
    }

我认为更简单的解决方案是来自jidesoft的StyledLabel并使用StyleRange。但StyledTableCellRenderer未包含在免费库中。 我也使用JTable,因为我需要可变的单元格高度。使用swt表无法实现这一点。

3 个答案:

答案 0 :(得分:2)

您需要使用更复杂的正则表达式模式来替换(并忽略 html 标记内的关键字)。 “快速而肮脏”的解决方案:

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

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

答案 1 :(得分:2)

可能有更好的方法,但基本上,这样做会设置一系列可选组,允许PatternMatcherString分解为“范围”

然后我们使用这些范围来“注入”规则......

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>

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

我要做的是创建一个可以带有条件("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);
    }

}

其中包含“条件”或“规则”以及要应用的格式。通常情况下,我可能会试图将“规则”和“格式化程序”分开,但我认为你可以得到基本的想法......

然后你可以做一些像......

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;
        }
    }
    //...
}

答案 2 :(得分:-1)

尝试 的setColor(BLACK); 在onject上。