highlight some words in the jeditorpane in java swing

1.3k Views Asked by At

I am working on a swing application in which i have to show HTML files in JEditorPane. I have to highlight some text e.g. For all occurrences of boy . I am using the following code but it is highlighting the whole text of JEditorPane :

try
{          
    javax.swing.text.DefaultHighlighter.DefaultHighlightPainter highlightPainter = 
        new javax.swing.text.DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
    textPane.getHighlighter().addHighlight(startPos, endPos, 
    highlightPainter);
}
catch(Exception ex)
{
}

but its highlighting the entire document . In this method what is the role of startPos and endPos ?

1

There are 1 best solutions below

3
On

you have to define the keywords first which means you need to iterate the whole content, when the words equal keyword, addHighLight it. You don't except the JeditorPane recoginise the keyword for you.