views:

110

answers:

2

I am displaying text in a Java JEditorPane using HTML to fomrat the text. I am also designing a search function that finds text in the JEditorPane selects the text and then scrolls to it. My problem is creating an algorithim that will actually specify the beginning and ending position for the selection.

If I simply retrieve the text using myeditorpane.getText(), then find the search string in the result, the wrong selection start and end positions are calculated with the wrong text being selected (the tags are throwing the calculation off). I tried removing the html tags by executing a replace all function text.().replaceAll("\<.*?>","") before searching for the text (this replace all removes all text in between the tags) but still the wrong selection points are calculated (although I'm getting close :-)).

Does anyone have an easy way to do this?

Thanks,

Elliott

+1  A: 

You probably want to be working with the underlying Document, rather than the raw text, as suggested in this HighlightExample.

trashgod
This gave me everything I needed. Thanks.
Elliott
A: 

You need to find the start location of the text. I guess something like:

int offset = editorPane().getDocument().getText().indexof(...);

Then to scroll you can use:

editorPane.scrollRectToVisible( editorPane.viewToModel(offset) );

Read up on Text and New Lines for more info.

camickr