使用该JTextPane
方法insertIcon()
时,javadoc 说明"...This is represented in the associated document as an attribute of one character of content."
如何检索有关我插入的图标的信息?我试过getCharacterAttributes()
只有"Fetches the character attributes in effect at the current location of the caret, or null."
是否存在一种方法来查找文本选择中的所有属性,或者在某个索引处,而不仅仅是在当前插入符号位置?
编辑
这是我拼凑起来的一些示例代码,用于获取嵌入图标的文件名。
Element root = jTextPane.getDocument().getDefaultRootElement();
BranchElement current = (BranchElement) root.getElement(0);
if (current != null)
{
Enumeration children = current.children();
while (children.hasMoreElements())
{
Element child = (Element) children.nextElement();
if (child.getName().equals("icon"))
{
AttributeSet attrSet = child.getAttributes();
ImageIcon icon = (ImageIcon) StyleConstants.getIcon(attrSet);
System.err.println(icon.getDescription());
}
}
}