2

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 ?

Kevin Reid
  • 21,218
  • 9
  • 61
  • 82
adesh
  • 1,067
  • 3
  • 18
  • 28
  • 2
    Please have a look at this wonderful [example](http://stackoverflow.com/a/9651404/1057230) by @mKorbel and this [example](http://stackoverflow.com/a/10309277/1057230), might be these will help. Moreover, you talking about `Swing` and not `awt` here, so better would be to add the tag for **Swing** too. – nIcE cOw Nov 12 '12 at 11:26
  • 2
    FWIW: Writing `catch(Exception ex){}` is making sure to give yourself a bad headache one day... Never write an empty "catch exception" block. Always leave a trace (a log, a message to the console...) – Guillaume Polet Nov 12 '12 at 13:30
  • 2
    Also [Highlight a word in jeditorpane](http://stackoverflow.com/questions/13448558/highlight-a-word-in-jeditorpane/13448656#13448656) – Bo PENG Nov 24 '12 at 05:15

1 Answers1

0

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.

EricLin
  • 1
  • 4
  • @driodev This *is* an answer to the question. It's not requesting clarification at all. – Servy Mar 08 '16 at 19:37
  • @Servy go to that review link and see. Its not only my comment – droidev Mar 09 '16 at 04:10
  • @driodev So because other people also chose the wrong action from review it's okay? That's not how that works. If I could reply to them as well, I would. – Servy Mar 09 '16 at 04:19