views:

1468

answers:

2

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?

+2  A: 
VonC
Thank you. This is very very nearly what I'm after. The only problem is that it interacts badly with selection highlighting (leaving blocks of apparently selected text hanging around after deselecting). For my purposes, if I was able to turn off selection entirely that would probably do the job.
Matthew Murdoch
Ok. I will check that later this day or tomorrow
VonC
I've found a solution for this (see my answer). Thanks.
Matthew Murdoch
+1  A: 

To prevent selection highlighting from interfering with VonC's solution I added the following line to the TextHighlight constructor (essentially making the selection highlight invisible):

textPane.setSelectionColor(new Color(1.0f, 1.0f, 1.0f, 0.0f));
Matthew Murdoch
Good catch! +1. I have updated my answer, and my own post on DZone Snippets, to include your line of code.
VonC
It does have the disadvantage however that it makes operations requiring text selection (cut, copy etc.) very difficult to use!
Matthew Murdoch