1 Attachment(s)
[JTextPane]Question regarding highligting text
Dear Sirs and Madams!
I am working on simple text editor, which functionality also includes Highlighting searched text. Here is situation: User loads text file into JTextPane. Then it selects Text Search tool and enters text to be searched. It confirms text search function with pressing "Mark All" button, which highlights all searched hits in text. But here is the problem - here is my code: Code:
@SuppressWarnings("unchecked")
private void stringOperationMarkAll(final String strSearch)
{
int iIndex=0;
int iNumberOccurences=0;
String strFileContent=this.textEditor().textArea().getText().toString();
String strRegExpInput="\\b"+strSearch+"\\b"; // constructs regular expression from searched string
Pattern p=Pattern.compile(strRegExpInput);
Matcher m=p.matcher(strFileContent);
CTextIndex cti=null;
ArrayList listTextIndexes=new ArrayList();
while(m.find())
{
iNumberOccurences++;
listTextIndexes.add(new CTextIndex(m.start(),
m.end()));
} // while
try
{
this.colorMatches(listTextIndexes,
Color.GREEN);
}
catch(BadLocationException ex)
{
Logger.getLogger(CFrameMain.class.getName()).log(Level.SEVERE, null, ex);
}
} // stringOperationMarkAll
private void stringOperationReplaceAll(final String strSearch,
final String strReplaceWith)
{
} // stringOperationReplaceAll
private void colorMatches(final ArrayList<CTextIndex> listMatches,
final Color c) throws BadLocationException
{
for(int iIndex=0; iIndex<listMatches.size(); iIndex++)
{
this.textEditor().textArea().getHighlighter().addHighlight(listMatches.get(iIndex).indexBegin(),
(listMatches.get(iIndex).indexEnd()-1),
new DefaultHighlighter.DefaultHighlightPainter(c));
} // for
} // color matches
In the first method, I gather start and end indexes of matched words in string, represented in JTextPane. The second method colorMathces highlights words, that are available in list of hits (parameter final ArrayList<CTextIndex>). The highlightaion shows, but in the wrong words. Why? I attached screenshot for windows file hosts and search parameter "127":Attachment 3397How do I translate string index to JTextPane coordinate?
Sincerely,
Marko
Re: [JTextPane]Question regarding highligting text
Go through camickr's blog entry on Text and New Lines.
db
Re: [JTextPane]Question regarding highligting text
THANKS MAN IT WORKS NOW!!!!! I am very grateful for helping me!!!!!:(grin):
Re: [JTextPane]Question regarding highligting text
Don't forget to thank camickr whenever you see him around.
db