2

I have created a Swing interface that uses a JTextPane. The JTextPane is highlighted with custom colors using:

textPane.getHighlighter().addHighlight(startPos, endPos, highlightPainter); 

The user is also able to highlight text with the cursor in the ordinary fashion.

My problem is that I can't figure out a way for text that is highlighted by both the highlighter object and cursor selection to be colored a third, different color. The highlighter object's highlighting always takes precedence.

I tried using a CaretListener object, but it only fires events when the user releases the mouse after highlighting manually. I need the overlap to render while the user is adjusting the highlighted region with the cursor.

I would even be happy with the cursor highlighting taking precedence over the highlighter object's highlightings instead, but the unique overlap color is a preferable feature.

The following question is similar to mine:

How to use LayeredHighlighter - One highlight on top of another

but the only answer just links to methods that overlay a GlassPane. I would much prefer a JTextPane or document-level solution, however, because the value of the selected text is important via

textPane.getSelectionStart();

and

textPane.getSelectionEnd();
Community
  • 1
  • 1
MattG
  • 1,416
  • 2
  • 13
  • 31
  • I don't think a `CaretListener` is enough, it will only tell about the highlight "after" the fact. You could attach a `ChangeListener` to the `Caret` directly. Check out [this example](http://stackoverflow.com/questions/18199593/in-java-swing-can-i-receive-caret-events-in-real-time/18200409#18200409) for some more details – MadProgrammer Aug 20 '13 at 23:43

2 Answers2

8

I would even be happy with the cursor highlighting taking precedence over the highlighter object's highlightings instead

JTextPane textPane = new JTextPane(...);
DefaultHighlighter highlighter =  (DefaultHighlighter)textPane.getHighlighter();
highlighter.setDrawsLayeredHighlights(false);
camickr
  • 321,443
  • 19
  • 166
  • 288
  • @MadProgrammer, I have to admit I don't really understand this property. I would think the default should be "false" as it does make sense to see the selected text. It also changes the way highlighting is done when you manually add a highlighter. Now if you manually highlight two lines of text the entire line will be highlighted even if text doesn't go to the end. When "true" is used only the text get highlighted. – camickr Aug 21 '13 at 01:32
0

I constructed the non-selection, custom highlighting colors with an alpha value for transparency (the default is complete opaqueness):

Color myColor = new Color( rValFloat, gValFloat, bValFloat, alpha);

This doesn't give me complete control of the overlapped region's color since the highlighting is a mix of the cursor highlighting and my color above, but I can also change the cursor's selection color with:

textPane.setSelectionColor(mySelectionColor);

which is enough control for my purposes.

MattG
  • 1,416
  • 2
  • 13
  • 31