user1565671
user1565671

Reputation: 23

Java JEditorPane Hyperlink won't exit tag

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.

Upvotes: 2

Views: 157

Answers (3)

Jill
Jill

Reputation: 539

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)

Upvotes: 0

StanislavL
StanislavL

Reputation: 57421

May be this http://java-sl.com/tip_autocreate_links.html could help

Upvotes: 5

trappski
trappski

Reputation: 1060

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.

Upvotes: 0

Related Questions