Help with highlighter and layering highlights and selections
HI
Hoping someone can help me layer highlights in a document.
I have implemented a find in the document that highlights all the instances of whatever the search word is and selects the one nearest the caret. This works very well. However, if you click somewhere else in the document the select is lost (correctly so) but I need that word (which was previously selected) to still be highlighted as it is still a correct instance of the found word.
I have tried adding a highlight (uncommenting the line below) and then the selection and setting the selection visible, but this only displays the highlight color and not the select. I need to layer the selection above the highlight so when the selection is moved, the highlight remains.
Any advice will greatly be appreciated.
JEditorPane currentTextPane;
//currentTextPane.getHighlighter().addHighlight(star t + foundPos, start + foundPos + s.length(), //editorHighlighter.highlightPainter);
currentTextPane.select(start + foundPos, start + foundPos + s.length());
currentTextPane.getCaret().setSelectionVisible(tru e);
Thanks
- By the way the other highlights are added with this code
currentTextPane.getHighlighter().addHighlight(star t + foundPos, start + foundPos + s.length(), editorHighlighter.highlightPainter);
Re: Help with highlighter and layering highlights and selections
If I understand correctly, you wish to be able to see the selection color AND highlight color when they overlap? Have you tried to use the alpha channel for the color of the highlighter? See: Highlighting Words in a JTextComponent (Java Developers Almanac Example) and instead of
Code :
Highlighter.HighlightPainter myHighlightPainter = new MyHighlightPainter(Color.red);
try
Code :
final Color highlight = new Color(255,0,0,100);//use the alpha channel in the color to allow colors to blend
Highlighter.HighlightPainter myHighlightPainter = new MyHighlightPainter(highlight);
Hope this is what you are looking for