从特定单词中删除突出显示 - Java [英] Removing Highlight from specific word - Java

查看:163
本文介绍了从特定单词中删除突出显示 - Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想构建一个示例,以便从与我的应用过滤器匹配的字词中删除高亮显示。因此,我基于以下示例:

I want to build one sample to remove highlight from words that matches the filter of my app. Therefore I was basing myself on the sample below:

    public void removeHighlights(JTextComponent jTextArea) {

        Highlighter hilite = jTextArea.getHighlighter();
        Highlighter.Highlight[] hilites = hilite.getHighlights();

        for (int i = 0; i < hilites.length; i++) {
            if (hilites[i].getPainter() instanceof TextHighLighter) {
                hilite.removeHighlight(hilites[i]);
            }
        }
    } 

此示例适用于全部删除文本区域的亮点。例如,如果我有三个选中的单词而我取消选中一个框,则所有过滤器都将被删除,我只想删除未选中单词的高亮显示。是否有一种简单的方法来检查哪个单词与过滤器匹配?或者我需要手动完成吗?直到现在我发现我的研究没什么成功

This sample works to remove all highlights of the Text Area. For example, if I have three selected words and I uncheck the box of one, all of the filters will be removed and I only want to remove the highlight of the unchecked word.. Is there a simple way to check which word matches the filter? or do I need to do it manually? cos until now I found nothing sucessful on my research

提前致谢

最终编辑:

根据@camickr为我工作的逻辑答案:

Logic working for me based on @camickr answer:

public void removeHighlights(JTextComponent jTextArea, String turnLightOff) {


           Highlighter hilite = jTextArea.getHighlighter();

           Highlighter.Highlight[] hilites = hilite.getHighlights();

           for (int i = 0; i < hilites.length; i++) {

              int wordLenght = hilites[i].getEndOffset() - hilites[i].getStartOffset();

              if(wordLenght == turnLightOff.length()){

                  if (hilites[i].getPainter() instanceof TextHighLighter) {

                      hilite.removeHighlight(hilites[i]);
              }

              }
           }                
}


推荐答案


有没有一种简单的方法来检查哪个单词与过滤器匹配?

Is there a simple way to check which word matches the filter?

否。


还是我需要手动完成吗?

or do I need to do it manually?

是的。每个突出显示都包含突出显示的开始/结束偏移。因此,您可以使用这些值从Document获取文本。如果文本匹配,则删除突出显示。

Yes. Each highlight contains the start/end offset of the highlight. So you use those values to get the text from the Document. If the text matches, then remove the highlight.

这篇关于从特定单词中删除突出显示 - Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆