2
votes

I am attempting to use hyperlinks inside of a JEditorPane for certain words, similar to the idea of clicking a word to get its definition. As words are typed into the editor pane, the program is checking them against a list and replacing listed words with an <a href="..." >some word</a> tag.

My problem is that after the word is replaced with the tag, I cannot get the editor pane to stop adding any following keystrokes to the text located inside the tag. How do I break out of the <a> tag to again start typing normally?

And yes, I am aware that hyperlink events only fire when the editor pane is not editable. I'm working around that with mouse events.

3

3 Answers

0
votes

I haven't worked with JEditorPanes myself but it sounds like the cursor position is still inside the inserted tag rather than outside. So a possible solution might be to move the cursor in front of the tag-position after replacing the word.

0
votes

play with the setCaretPosition method on the JEditorPane; get the position of the end of the tag; try something like this:

HTMLDocument document = (HTMLDocument)editor.getDocument();
 int caretPos = editor.getCaretPosition();

 Element elem = document.getParagraphElement(caretPos);

 int pos = elem.getEndOffset();
 editor.setCaretPosition(pos)

being careful to check the length of the document, not setting the caret to a bad position.

also, looking at source code of wysiwg java editors helps (like shef)