soham
soham

Reputation: 1676

How highlighter of Java Swing works on marginal cases?

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) {
            }

Upvotes: 0

Views: 904

Answers (3)

soham
soham

Reputation: 1676

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.

Upvotes: 0

camickr
camickr

Reputation: 324207

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);

Upvotes: 1

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81734

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.

Upvotes: 0

Related Questions