在存在重复行的情况下在JTextArea中突出显示文本 [英] Highlight Text In JTextArea In Presence of Duplicate Lines

查看:92
本文介绍了在存在重复行的情况下在JTextArea中突出显示文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JTextArea,由行组成(其中一些可能重复).我需要在右键单击时突出显示所选行.我用来突出显示的代码如下:

I have a JTextArea which consists of lines (some of them possibly duplicates of one another). I've a requirement where I've to highlight the selected line upon right-click. The code that I'm using to highlight is as follows:

String highlightedText = textArea.getSelectedText();
if(highlightedText != null)
{
   try{
      int index = textArea.getText().indexOf(highlightedText, textArea.getCaretPosition());
      textArea.getHighlighter().addHighlight(index - 1, index + highlightedText.length(),
      new DefaultHighlighter.DefaultHighlightPainter(Color.ORANGE));
   }catch (BadLocationException ex) {           
   }
}

当右键单击突出显示时,问题是在存在重复项的情况下我无法获取所选行的索引.因此,如果有类似

While the highlight upon right-click works, the problem is I cannot get the index of the selected line in the presence of duplicates. So if there're lines like

AAAA
BBBB
AAAA
CCCC
DDDD
AAAA

AAAA
BBBB
AAAA
CCCC
DDDD
AAAA

当用户尝试突出显示此特定行时,我无法获取第二个"AAAA"的索引.有人可以帮我解决这个问题吗?谢谢!

I cannot get the index of the second "AAAA" when the user tries to highlight this particular line. Can someone help me out with an idea/suggestion to work this this? Thanks!

推荐答案

您几乎完全拥有了它,但是问题很少.

You had it almost all by yourself there were few issues though.

  1. 您应该使用getSelectionStart()而不是getCaretPosition().
  2. 突出显示应该从index开始,而不是从index-1开始.
  1. You should use getSelectionStart() rather than getCaretPosition().
  2. Highlight should start from index not from index-1.

请参见下面的示例.选择要突出显示的内容,右键单击textArea或按按钮突出显示您的选择:

Please see the example below. Select what you want to highlight right click on the textArea or press the button to highlight your selection:

public class HighlightingTextArea {    
    public static void main(String[] args) {    
        SwingUtilities.invokeLater(new Runnable() {    
            @Override
            public void run() {
                final JTextArea textArea = new JTextArea(10, 44);
                textArea.append("AAAA\nBBBB\nAAAA\nCCCC\nDDDD\nAAAA");
                JButton b = new JButton(new AbstractAction("highlight") {    
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        highlightTextAreaSelection(textArea);
                    }
                });
                textArea.addMouseListener(new MouseAdapter() {    
                    @Override
                    public void mousePressed(MouseEvent e) {
                        super.mousePressed(e);
                        if (e.getButton() == MouseEvent.BUTTON3) {
                            highlightTextAreaSelection(textArea);
                        }
                    }
                });
                JPanel panel = new JPanel(new BorderLayout());
                panel.add(textArea);
                panel.add(b, BorderLayout.SOUTH);
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setContentPane(panel);
                f.pack();
                f.setVisible(true);
            }
        });
    }

    private static void highlightTextAreaSelection(JTextArea textArea) {
        String highlightedText = textArea.getSelectedText();
        if (highlightedText != null) {
            try {
                int index = textArea.getText().indexOf(highlightedText, textArea.getSelectionStart());
                textArea.getHighlighter().addHighlight(index, index + highlightedText.length(),
                        new DefaultHighlighter.DefaultHighlightPainter(Color.ORANGE));
            } catch (BadLocationException ex) {
            }
        }
    }
}

这篇关于在存在重复行的情况下在JTextArea中突出显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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