当使用 JTextPane
方法 insertIcon()
时,javadoc 指出 "...这在相关文档中表示为内容的一个字符。”
如何检索有关我插入的图标的信息?我已经尝试过 getCharacterAttributes()
,它只“获取在插入符当前位置有效的字符属性,或者为 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());
}
}
}
最佳答案
使用文档的元素获取属性:
Element root = textComponent.getDocument().getDefaultRootElement();
一旦你有了根元素,你就可以得到与你选择的文本相关的元素。首先在您的起始偏移量处找到元素,然后继续循环遍历每个元素,直到到达结束偏移量。
关于java - 检索 JTextPane AttributeSet 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4207673/