Question

My Question is quite simple.

I have a JtextArea. My aim is to highlight a line in that textarea from the start of the first character in that line.. By using Java line start and end offset, am able to highlight the whole line, but it looks ugly.

Am trying to get all text from that line.Normally it will contain spaces. What I tried :

String text = textArea_1.getText(); //My TextArea
int startIndex = textArea_1.getLineStartOffset(i11); //i11 is the Line Number
int endIndex = textArea_1.getLineEndOffset(i11);
String myString = text.substring(startIndex, endIndex);
//I get all text from that line

for(int i1 = 0; i1 < myString.length(); i1++){ 
//This is trying to increment startindex until it gets a non empty space, 
//the counter should stop.
       while(Character.isWhitespace(myString.charAt(i1))){
              startIndex++;
              break;
       }
}

String myString2 = text.substring(startIndex, endIndex);
//I now pass the new startIndex
String [] javaWords = {myString2};//Function highlight takes an array.
System.out.println("String to Highlight: " + myString2);
highLight(textArea_1, javaWords);//Function to Highlight  

The Highlighter Method:

public 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());
     for (int i = 0; i < pattern.length; i++) {
        int pos = 0;
         // Search for pattern
         while ((pos = text.indexOf(pattern[i], pos)) >= 0) {
            hilite.addHighlight(pos, pos + pattern[i].length(),painter);
            pos += pattern[i].length();
         }
     }
     } catch (BadLocationException e) {}
}

The result of the code above: enter image description here

in x;
string y;
chr z; 

The above should be highlighted. Am I missing something?

Was it helpful?

Solution

The Issue was with the loop..Got it Fixed:

String text = textArea_1.getText();
                        int startIndex = textArea_1.getLineStartOffset(i11);
                        int endIndex = textArea_1.getLineEndOffset(i11);
                        String myString = text.substring(startIndex, endIndex);
                        for(int i1 = 0; i1 < myString.length(); i1++){
                            if(Character.isWhitespace(myString.charAt(i1))){
                                startIndex++;

                            }
                         if(!Character.isWhitespace(myString.charAt(i1))){
                                startIndex = startIndex+0;;
                                break;
                            }


                        }

                        String myString2 = text.substring(startIndex, endIndex);
                        String [] javaWords = {myString2};
                        System.out.println("String to Highlight: " + myString2);
                        highLight(textArea_1, javaWords);    

Result:

enter image description here

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