Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help with highlighter and layering highlights and selections

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Thanked 0 Times in 0 Posts

    Default Help with highlighter and layering highlights and selections

    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);


    - By the way the other highlights are added with this code
    currentTextPane.getHighlighter().addHighlight(star t + foundPos, start + foundPos + s.length(), editorHighlighter.highlightPainter);

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 833 Times in 772 Posts
    Blog Entries

    Default 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

    Highlighter.HighlightPainter myHighlightPainter = new MyHighlightPainter(Color.red);


    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

  3. The Following User Says Thank You to copeg For This Useful Post:

    MazOz (December 7th, 2009)

Tags for this Thread