Search code examples
javaswingjtextpanecarethtmleditorkit

caret position of an embedded swing component in a JTextPane


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?


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.