JTextPane and paintComponent override

You are here:

afternoon folk,
i have an rtf document rendered in a JTextPane. within the document are a couple of keywords that i want to have a visual highlight applied to (a line drawn just above them across the text area width. i am trying to come up with the best solution for this, as the document is an editable document, and hence the content either before or after the keywords will change, and the highlight must also change with it. i have about gotten it by tracking changes to the document, and tracking the line (relative to the start of the document) that the keyword is located in. my problem is two-fold. one is i can't seem to get the proper location offsets for using the draw Graphics2D.draw() method invocation. a code snippet of how i'm locating the offset in the text buffer, and then converting it to what i had hoped would be the proper coordinate space is as follows:
int yMarker0 = 0;
int yMarker1 = 0;
...

when i hit the paintComponent(), the lines don't show up at the line the keywords are on, but rather much higher up (toward the top) in the document. the line numbers that are being displayed in the println calls are the correct line numbers though, and the font metrics seems to be correct. this feels like i'm fixing to have one of those duh moments once i hear what i'm doing wrong (or i happen to stumble across it).

the bigger problem is that i've spent about 12 hours searching and hunting for the proper way to accomplish this (the time spent isn't the problem), but i'm not sure i am really approaching it properly. i am might weak in swing programming, and i have wondered if i shouldn't be pursuing the swing highlighters route as a better approach, but even there, i'm not sure.

I would not highlight text via paintComponent override. There is support for character/text manipulation in document views/renderers. Once I had to solve similar problem and ended up writing my own ParagraphView (or BoxView or some other view ... don't remember exactly) and parsing the document and setting character attributes which worked quite nicely. There were some minor issues like if highlight started in middle of the word, new character attribute group confused algorithm for line wrapping and it was breaking the word at the highlight boundary, but this could have been dealt with if needed as well (manually controlling the line break markers). I can try to dig out that code and make some small demo out of it if that sounds interesting, but it would take few days ...