开发者

Multiple viewports onto the same JTextArea?

There is a component I would like to make which shows all matches of a set of words in a JTextArea, along with some context (N lines, probably user-configurable.)

I already have the code for highlighting the matches so I know their offsets and can therefore determine their bounds. I know the line height of the text so I can determine the bounds of the context areas I want to paint.

But is there some class like JViewport onl开发者_开发技巧y able to paint multiple views of the underlying component? Since JViewport is a normal Swing component, as soon as you put the same text area under another viewport, it gets detached from the first one.

Edit: Actually as it turns out, I can't seem to determine the bounds of the context areas as the lines-to-offset mapping JTextArea gives me doesn't count wrapped lines.


The problem here is that JViewport is a normal container, and treats its view component as a child component (and each component can only be child in one container). This is quite reasonable, given that always only one component can have the focus, and similar things.

I don't know of any JViewport-like class that would do what you want (painting a view of a component without being parent of it), but in your case, you can achieve most of the same by using two JTextAreas sharing the same Document. You then only would have to synchronize the caret movements (if so wished), I think.


There is nothing that exists as you describe. In fact it sounds like you want to customize the display of a JTextArea. In which case you can override and change the way JTextArea paints itself. Either painting more on top, or changing it completely.


Paint your JTextArea in an Image and use necessary fragments of the image. Keep the image's bounds in original JTextArea so clicking bu the image you can calculate click on the JTextArea to get proper position.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜