Question

I have a JTextPane which uses a custom HTMLEditorKit to embed a swing component when the HTML tag is used. When I click on this component, I want to have the caret position updated to the location of this component. I can get the focus event in the control, but I don't know how to set the caret position to the location of this component. Anyone know how?

Was it helpful?

Solution

You can get clicked component's parent and use parent.getBounds(). The rectangle returned is the position in JTextPane. The use viewToModel() method of JTextPane to get caret position for the coordinates location.

OTHER TIPS

You'll need the character position of the embedded component within the JTextPane's content; without more details, there's no way to guess how you would determine that. However, once you know the character position of the component, it's as easy as:

myTextPane.setCaretPosition(characterIndexOfEmbeddedComponent);

See: http://docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#setCaretPosition(int)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top