Updating an Attribute of a Component embedded in an AbstractDocument
I'm inserting a component into an AbstractDocument as shown in the code below.
final MutableAttributeSet aS = new SimpleAttributeSet();
aS.addAttribute(Utils.STYLE_ATTRIBUTE, attributeValue);
Component myComponent = new MyComponent();
myComponent.addM开发者_Python百科ouseListener(l);
StyleConstants.setComponent(aS, myComponent);
insertString(caretPosition, REPLACEMENT_CHARACTER, aS);
Note that I'm also adding a mouse listener to this component. When the user double-clicks on the component embedded in the document, I wish to change the value of the Utils.STYLE_ATTRIBUTE attribute in the associated attribute set in the document to a new value.
How do I do this? How can I get a handle to this attribute set or the element containing the attribute set?
You can get the component's bounds in the mouseListener (use e.getSource()). Then use viewToModel() method of JEditorPane to get offset in the Document for the bounds' position. Then get leaf Element (character element) from the Document's structure and change style.
精彩评论