0

I have this code :

JTextArea textComp;

Highlighter hilite = textComp.getHighlighter();

if (word.toString().equals(pattern[i])) 
{
    hilite.addHighlight(posStart, (posEnd), myHighlighter);
    break;
}

word is a StringBuilder

Suppose the condition in if matches and hilite.addHighlight(posStart, (posEnd), myHighlighter); - this statement is going to execute. Then the textComp contains

int myVar

And I try to highlight like this

int myVar

At that time, posStart = 0 and posEnd = 3. But As I am entering something into the textArea the highlighter is extending itself to the end like this:

int myVar

Can anyone help me with this?

And if I make the statement:

hilite.addHighlight(posStart, (posEnd-1), myHighlighter);

Then with posStart=0, posEnd=3, then only

*in*t myVar this happens. i.e "in" is highlighted but "t" is not!

EDIT The function:

Highlighter.HighlightPainter myHighlighter = new MyHighlightPainter(
                    Color.LIGHT_GRAY);

            try {
                Highlighter hilite = textComp.getHighlighter();
                Document doc = textComp.getDocument();
                String text = doc.getText(0, doc.getLength());
                String[] words = text.split(" ");

                                int posEnd, posStart = 0;
                while (text.length() > posStart) {
                    posEnd = posStart;
                    StringBuilder word = new StringBuilder();
                    while (posEnd < text.length() && text.charAt(posEnd) != ' ') {
                        word.append(text.charAt(posEnd++));
                    }
                    for (int i = 0; i < pattern.length; i++) {

                        if (word.toString().equals(pattern[i])) {
                            hilite.addHighlight(posStart, (posEnd-1), myHighlighter);
                            break;
                        }

                    }
                    posStart = posStart + posEnd + 1;
                }
            } catch (BadLocationException e) {
            }
Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
soham
  • 1,508
  • 6
  • 30
  • 47
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jun 13 '13 at 12:40
  • I have 165lines of code. Can I give it? I can point out the concerned area. Although I have mentioned the same here also. @AndrewThompson – soham Jun 13 '13 at 12:42
  • 1
    Could that be of any help? http://stackoverflow.com/questions/14229611/code-completion-and-syntax-highlighting-in-swing The first answer might solve what you want. – Stelios Adamantidis Jun 13 '13 at 12:43
  • *"I have 165lines of code. Can I give it?"* I think 165 LOC counts as 'short'. As long as it demonstrates the problem, that would make it an SSCCE. – Andrew Thompson Jun 13 '13 at 12:44
  • Okay, I guess it's a little big! But I'll share relevant code. @SteliosAdamantidis, I tried it, but somehow, it isn't happening!! – soham Jun 13 '13 at 12:51
  • @soham.m17 before anything to read Oracles tutorial about `JTextComponen`t, where is described about `how to search in JTextComponents`, use `JTextPane` for multihighlight & multicoloring, is easier, than by using `JTextArea`, `HighLighter` are stored in array, then have to loop into `HighLighter[]`, have to play with `if - instanceof myHighLighter - then whatever`, only – mKorbel Jun 13 '13 at 12:59
  • @mKorbel thanks, actually I was wondering how this Highlighter works! I am instructing it to highlight from `0` to `2` index. But it's highlighting only `2` characters i.e `in`t although `0` to `2` is `int`. – soham Jun 13 '13 at 13:03

3 Answers3

1

actually I was wondering how this Highlighter works!

Yes the API is confusing. I believe the first index is meant to be inclusive and the last index exclusive.

Whenever I highlight when searching for a word I use code like:

String text = "one two three four...";
String search "three";

int offset = text.indexOf(search);

if (offset != -1)
    textPane.getHighlighter().addHighlight(offset, offset + search.length(), painter);
soham
  • 1,508
  • 6
  • 30
  • 47
camickr
  • 321,443
  • 19
  • 166
  • 288
  • And if you delete backwards till "three" and then again type something, the highlighter extends itself. Like if you make the text "one two threeFive" by typing in the `JTextArea` or something like that, then `threeFive` is highlighted. – soham Jun 13 '13 at 21:18
  • Yes, that also is the default behaviour. – camickr Jun 13 '13 at 22:03
  • Well, So I've achieved my goal in another way. I'll post that here!! – soham Jun 14 '13 at 07:17
0

Your issue seems to not really be how the highlighter highlights text, but rather how the highlight changes as the text is edited. You can deal with this by adding a change listener to the TextArea, and adjusting the affected highlights (using changeHighlight()) as needed.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
0

I have done this to solve the problem:

int wordEnd = 0, wordStart = 0;




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

                        while (text.length() > wordEnd) 
                        {
                            String word = new String();
                            if(text.charAt(wordEnd) == ' ')
                            {
                                word = text.substring(wordStart, wordEnd);
                                for (int i = 0; i < pattern.length; i++) 
                                {
                                        if (word.toString().equals(pattern[i])) 
                                        {
                                            hilite.addHighlight(wordStart, (wordEnd), myHighlighter);

                                            break;
                                        }
                                }
                                wordStart = wordEnd + 1;
                            }
                            wordEnd++;
                        }

What I did, is to getting a word by detecting a space after it(you can extend the logic) and then highlighting the previous word. This is continuous all through typing.

soham
  • 1,508
  • 6
  • 30
  • 47