Question

Am having a method that highlights all the occurrences of a word in a Text Area. Is there any way to use start and end offset to highlight the word only in that line.

My current codes.

public static void highlight(JTextComponent textComp, String pattern) {

    try {
        Highlighter hilite = textComp.getHighlighter();
        javax.swing.text.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(), painter2);
            pos += pattern.length();
        }
    } catch (BadLocationException e) {
    }
}

This is called on a JButton action performed:

highlight(textArea_1, "in");

The result: enter image description here

I tried using the Start and End Offset Method but with no luck.I tried to highlight "in" on line 6 only.Any Help on this much appreciated.

static int iline =6;

    public static void highlight(JTextComponent textComp, String pattern) {

    try {
        Highlighter hilite = textComp.getHighlighter();
        javax.swing.text.Document doc = textComp.getDocument();
        int start =((JTextArea) textComp).getLineStartOffset(iline);
        int end = ((JTextArea) textComp).getLineEndOffset(iline);
        String text = doc.getText(start,end);
        int pos = start;

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

Solution 2

Got it to work Thanks @camickr

Code:

public static void highlight(JTextComponent textComp, String pattern) {

    try {
        Highlighter hilite = textComp.getHighlighter();

        String text = textComp.getText();
        String line =null;
        int start = 0;
        int end;
        int totalLines = ((JTextArea) textComp).getLineCount();
        for (int i=0; i < totalLines; i++) {

            if(i==5){ //Line Numbers Decrement by 1
            start = ((JTextArea) textComp).getLineStartOffset(i);
            end   = ((JTextArea) textComp).getLineEndOffset(i);
            line = text.substring(start, end);
            System.out.println("My Line: " + line);
            System.out.println("Start Position of Line: " + start);
            System.out.println("End Position of Line: " + end);
            }
        }
        int pos = start;

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

OTHER TIPS

int start =((JTextArea) textComp).getLineStartOffset(iline);
int end = ((JTextArea) textComp).getLineEndOffset(iline);
String text = doc.getText(start,end);
int pos = start;

Lets assume we have 10 character on every line. So if you want the text on the 6th line the start variable will be equal to 51 and the end variable 60.

When you get the text for the 6th line it will only contain 10 characters.

So when you start the search at 51, you are already at the end of the search string so you won't get any matches.

You need to just start the search at offset 0, and then when you add the highlight you need to add the value of start to the highlight offsets.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top