I am trying to highlight a specific lines from JTextPane
. Suppose I want to highlight the 5th line from JTextPane
, how do I get the indexOf
it to highlight it if the lines are same?
Example content of JTextPane
, I want to higlight 5th and 11th line from below lines,
This text is from stackoverflow This text is from stackoverflow This text is from stackoverflow This text is from stackoverflow This text is from stackoverflow This text is from stackoverflow This text is from stackoverflow This text is from google This text is from yahoo This text is from yahoo This text is from yahoo This text is from yahoo
Code:
//Code to highlight
//text is jtextpane
final static Color HILIT_COLOR = Color.LIGHT_GRAY;
DefaultHighlighter hilit = new DefaultHighlighter();
DefaultHighlightPainter painter = new
DefaultHighlighter.DefaultHighlightPainter(HILIT_COLOR);
text.setHighlighter(hilit);
hilit.removeAllHighlights();
String s = text.getText();
try {
hilit.addHighlight(0, 10, painter);
} catch (BadLocationException ex) {
Logger.getLogger(TextLines.class.getName()).log(Level.SEVERE, null, ex);
}