I have a JTextArea
which consists of lines (some of them possibly duplicates of one another). I've a requirement where I've to highlight the selected line upon right-click. The code that I'm using to highlight is as follows:
String highlightedText = textArea.getSelectedText();
if(highlightedText != null)
{
try{
int index = textArea.getText().indexOf(highlightedText, textArea.getCaretPosition());
textArea.getHighlighter().addHighlight(index - 1, index + highlightedText.length(),
new DefaultHighlighter.DefaultHighlightPainter(Color.ORANGE));
}catch (BadLocationException ex) {
}
}
While the highlight upon right-click works, the problem is I cannot get the index of the selected line in the presence of duplicates. So if there're lines like
AAAA
BBBB
AAAA
CCCC
DDDD
AAAA
I cannot get the index of the second "AAAA" when the user tries to highlight this particular line. Can someone help me out with an idea/suggestion to work this this? Thanks!