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.

3
  • Ok, and what is the problem? Any errors? Do you have SSCCE to reproduce the problem? Commented Jun 10, 2016 at 6: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. Commented Jun 10, 2016 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. Commented Jun 10, 2016 at 10:42

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, I'm a bit confused here. Do I comment out the removeHighlights(textComp); as it removes previous ones?
Thank you. I saw my mistake which was inserting the pane.setText(pane.getText() +"\n "); I removed that and commented out the removeHighlight method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.