Search code examples
javalinehighlightingjeditorpane

How do I get a JEditorPane to highlight the full width of a line (not just the text)?


I'm trying to get a JEditorPane to highlight the full width of a displayed line. All the examples I've tried only highlight the textual content. For example if I have content such as this:

 ---------------------------------
|Here is some text                |
|some more text                   |
 ---------------------------------

within a JEditorPane represented by the box above, then highlighting the first row highlights only the 'Here is some text' (represented between [ and ] below).

 ---------------------------------
[Here is some text]               |
|some more text                   |
 ---------------------------------

I would like it to highlight the full width of the JEditorPane like the following:

 ---------------------------------
[Here is some text                ]
|some more text                   |
 ---------------------------------

Is there a way to do this?


Solution

  • This seems not too complicated. I would make that a little code-challenge.

    Just create your own custom Highlighter, extending DefaultHighlighter.

    Override the paint() method, and simply leave unchanged the width of the rectangle paint: it will be the width of the panel.

    You will find in DZone Snippets a complete example: copy it and run it. Tell me if this is what you are after. It includes the textPane.setSelectionColor(new Color(1.0f, 1.0f, 1.0f, 0.0f)); addition you mention in your answer.

    alt text http://www.freeimagehosting.net/uploads/94a3a990e4.png