Search code examples
javaswingjtextareahighlighting

Highlight JTextArea


i have a problem with:

        int index = text.indexOf(myWord);

        while (index >= 0) {
            int len = pattern.length();
            hl.addHighlight(index, index + len, DefaultHighlighter.DefaultPainter);
            index = text.indexOf(pattern, index + len);
        }

when I reach the end of line highlighting is still enable, how to disable highlight ? The result of above code:

enter image description here

Thanks for answer but the problem is:

enter image description here

The black line is a place where I clicked and start typing, it is just bug.


Solution

  • Well I had to do some assumptions on your variable names and I hope I did it right. Basically your code worked for me but I didn't understand the first line:

    int index = text.indexOf(myWord);
    

    I think it should be:

    int index = text.indexOf(pattern);
    

    Anyway, I wrote this code and it worked for me:

    String pattern = "<aa>";
    String text = textArea.getText();        
    int index = text.indexOf(pattern);
    while(index >= 0){
        try {                
            hl.addHighlight(index, index + pattern.length(), DefaultHighlighter.DefaultPainter);
            index = text.indexOf(pattern, index + pattern.length());            
        } catch (BadLocationException ex) {
            ex.printStackTrace();
        }
    }
    

    Update

    Look when you click on the end of marked text and start typing the text will be also marked- it is a my bug.

    Sorry I didn't see that before. I don't think you can stop a highlighter but you can make it using a CaretListener and doing the stuff there. This way if you input a new string that matches your pattern it will be highlighted too:

    textArea.addCaretListener(new CaretListener() {            
        @Override
        public void caretUpdate(CaretEvent e) {
            if(e.getMark() == e.getDot()){
                Highlighter hl = textArea.getHighlighter();
                hl.removeAllHighlights();
    
                String pattern = "<aa>";
                String text = textArea.getText();        
                int index = text.indexOf(pattern);
                while(index >= 0){
                    try {                
                        Object o = hl.addHighlight(index, index + pattern.length(), DefaultHighlighter.DefaultPainter);
                        index = text.indexOf(pattern, index + pattern.length());
                    } catch (BadLocationException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        }
    });
    

    Note e.getMark() == e.getDot() means "there's no text selection" based on CaretEvent.getDot() and CaretEvent.getMark() javadoc. Otherwise you shouldn't highlight anything but selected text.

    Screenshot

    enter image description here

    Updated SSCCE

    Here's a complete SSCCE to test it:

    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.SwingUtilities;
    import javax.swing.event.CaretEvent;
    import javax.swing.event.CaretListener;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.DefaultHighlighter;
    import javax.swing.text.Highlighter;
    
    public class Demo {
    
        private void initGUI(){
    
            final JTextArea textArea = new JTextArea();
    
            textArea.addCaretListener(new CaretListener() {            
                @Override
                public void caretUpdate(CaretEvent e) {
                    if(e.getMark() == e.getDot()){
                        Highlighter hl = textArea.getHighlighter();
                        hl.removeAllHighlights();
    
                        String pattern = "<aa>";
                        String text = textArea.getText();        
                        int index = text.indexOf(pattern);
                        while(index >= 0){
                            try {                
                                Object o = hl.addHighlight(index, index + pattern.length(), DefaultHighlighter.DefaultPainter);
                                index = text.indexOf(pattern, index + pattern.length());
                            } catch (BadLocationException ex) {
                                ex.printStackTrace();
                            }
                        }
                    }
                }
            });
    
            JScrollPane scrollPane = new JScrollPane(textArea);
            scrollPane.setPreferredSize(new Dimension(300,200));
    
            JPanel content = new JPanel(new FlowLayout());
            content.add(scrollPane);
    
            JFrame frame = new JFrame("Demo");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setContentPane(content);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);        
        }
    
    
        public static void main(String[] args) {        
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new Demo().initGUI();
                }
            });
        }
    
    }