0

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.

Love
  • 43
  • 2
  • 10
  • Ok, and what is the problem? Any errors? Do you have SSCCE to reproduce the problem? – StanislavL Jun 10 '16 at 06:00
  • I've currently put `frame.add(pane); frame.pack(); frame.setVisible(true); highlight(pane,mtch);` inside the while brackets and it currently highlights only the last word of the matching text inside the original text. I'm trying to highlight all matching text not just the last line. – Love Jun 10 '16 at 10:38
  • So for example, the code opens a window with the text: `Hello, my name is John and I live in a box` and the only highlighted word is `in` as it is the last word of the matching text. I'm trying to highlight all matching words. – Love Jun 10 '16 at 10:42

1 Answers1

0

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

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • Sorry, I'm a bit confused here. Do I comment out the `removeHighlights(textComp);` as it removes previous ones? – Love Jun 10 '16 at 12:57
  • Thank you. I saw my mistake which was inserting the `pane.setText(pane.getText() +"\n ");` I removed that and commented out the `removeHighlight` method. – Love Jun 10 '16 at 13:15