javajeditorpanejtextcomponent

How to highlight matching words from two text files?


I have two text files which I'm trying to compare, i.e., find matching words and highlight the matching words. The code below takes the two files, finds matching words and prints them out:

public class Test {

static MyHighlightPainter myHighlightPainter = new MyHighlightPainter(Color.red);

public static void main(String[] args) throws Exception {
    //BufferedReader db = new BufferedReader(new FileReader("src/project/test1.txt"));

            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JEditorPane pane = new JEditorPane();

               File file = new File("src/project/test1.txt");
               Scanner fileScan = new Scanner(file);

               File file1 = new File("src/project/test.txt");
            Scanner file1Scan = new Scanner(file1);

            Set<String> db = new HashSet<>();
            Highlighter highlighter = pane.getHighlighter();


            while(fileScan.hasNextLine()){
                db.add(fileScan.nextLine());}
                while(file1Scan.hasNextLine()){
                    String mtch = file1Scan.nextLine();
                    if(db.contains(mtch)){                          
                        pane.setPage(file.toURI().toURL());
                        pane.setText(pane.getText() +"\n ");
                        System.out.println(mtch);
                        frame.add(pane);
                        frame.pack();
                        frame.setVisible(true);
                        highlight(pane,mtch);

                    }


        //

        }


};

For example, 'test.txt' contains: Hello, my name is John and I live in a box. and 'test1.txt' contains: John does not live in France.

This prints out(system.in.println) John, in and live. The pane.setPage(file.toURI().toURL()); prints out everything in 'test.txt' in a window so that's two different outputs.

What I'm trying to achieve is to highlight the matching words admist the original text. So, Hello, my name is John and I live in a box. would have John, in and live highlighted.

I'm testing using a highlight using a highlight method I found on the web:

public static void highlight(JTextComponent textComp, String pattern) {
        // First remove all old highlights
        removeHighlights(textComp);

    try {
        Highlighter hilite = textComp.getHighlighter();
        Document doc = textComp.getDocument();
        String text = doc.getText(0, doc.getLength());
        int pos = 0;

        // Search for pattern
        while ((pos = text.indexOf(pattern, pos)) >= 0) {
            // Create highlighter using private painter and apply around pattern
            hilite.addHighlight(pos, pos + pattern.length(), myHighlightPainter);
            pos += pattern.length();
        }
    } catch (BadLocationException e) {
    }
}

// Removes only our private highlights
public static void removeHighlights(JTextComponent textComp) {
    Highlighter hilite = textComp.getHighlighter();
    Highlighter.Highlight[] hilites = hilite.getHighlights();

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

class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter {

public MyHighlightPainter(Color color) {
    super(color);
}

I've tried highlight(pane,mtch) and that prints out the contents of the text file.


Solution

  • You call highlight(pane,mtch); so it's called for each word.

    The highlight() removes all previous ones by the call

    // First remove all old highlights
    removeHighlights(textComp);
    

    thus the only last word is highlighted