检索 JTextPane AttributeSet 值

发布于 2024-10-03 04:24:06 字数 941 浏览 9 评论 0原文

当使用 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());
        }
    }
}

When using the JTextPane method insertIcon(), the javadoc states "...This is represented in the associated document as an attribute of one character of content."

How do I retrieve the information about my inserted icons? I have tried getCharacterAttributes() which only "Fetches the character attributes in effect at the current location of the caret, or null."

Does a method exist to find all of the attributes in a selection of text, or at a certain index, not just at the current caret position?

Edit
Here is some sample code I pieced together to obtain the filename of an embedded icon.

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());
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烏雲後面有陽光 2024-10-10 04:24:06

使用文档的元素获取属性:

Element root = textComponent.getDocument().getDefaultRootElement();

一旦获得根元素,您就可以获得与所选文本相关的元素。首先找到起始偏移处的元素,然后继续循环每个元素,直到到达结束偏移。

Use the Element of the Document to get the attributes:

Element root = textComponent.getDocument().getDefaultRootElement();

Once you have the root Element you can get the Elements related to your selected text. Start by finding the Element at your start offset and then keep looping through each Element until you reach the end offset.

氛圍 2024-10-10 04:24:06
StyledDocument doc=(StyledDocument)textComponent.getDocument();
int selStart=textComponent.getSelectionStart();
int selEnd=textComponent.getSelectionEnd();

然后使用 doc.getCharacterElement() 方法传递 start 来获取第一个字符 elem。然后使用 elem getEndOffset() 可以获得下一个 char 元素。检查 elem 开始和结束偏移量是否小于选择结束。

StyledDocument doc=(StyledDocument)textComponent.getDocument();
int selStart=textComponent.getSelectionStart();
int selEnd=textComponent.getSelectionEnd();

Then use doc.getCharacterElement() method passing start to get first char elem. Then using the elem getEndOffset() you can get the next char element. Check the elem start and end offset to be less then selection end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文