从JTextPane通过javax.swing.text.Element获取组件? [英] Get a component from a JTextPane through javax.swing.text.Element?

查看:110
本文介绍了从JTextPane通过javax.swing.text.Element获取组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 JTextPane 来显示字符和符号,后者由自定义绘制的 JComponents 表示。例如,文本窗格可能显示如下内容:

文本窗格是用户可编辑的,允许用户通过任何位置的按钮添加更多符号,并替换所选文本。我是通过 JTextPane.insertComponent()方法完成的。在应用程序的某个时刻,我需要知道文本窗格中当前显示的内容,并且我不仅指输入的文本,还指其中包含的确切组件。

I am using a JTextPane to display characters and symbols, where the latter are represented by custom painted JComponents. For example, the text pane might show something like this: The text pane is user editable and it is allowed for the user to add more symbols via a button at any position and as a replacement for selected text. I do this via the JTextPane.insertComponent() method. At some point in the application I need to know what is currently being displayed in the text pane, and by that I mean not only the entered text, but also the exact components contained within.

我经历了 Positions DocumentListeners 的广泛麻烦来管理我的文本窗格的内容,但是我不断引起比我解决的更多问题。这就是为什么我最终决定,我的麻烦可能是由于我的设计错误,所以我决定看看,如果我无法通过文本窗格访问我的组件。

I went through extensive troubles with Positions and DocumentListeners to manage the content of my text pane, but I kept causing more problems than I was solving. That is why I finally decided, that my troubles are probably due to a design fault on my part, so I decided to see, if I can't get to my components through the text pane.

搜索 AbstractDocument 的文档和源代码以及其他相关类,我找到了接口 javax.swing.text。元件。然后我让我的应用程序输出

Searching through the documentation and the source code of AbstractDocument and other related classes, I found the interface javax.swing.text.Element. I then let my application output

for(int i = 0; i < textPane.getDocument().getLength(); i++) {
    System.out.println(((StyledDocument) textPane.getDocument()).getCharacterElement(i));
}

给了我:


LeafElement(内容)0,4

LeafElement(content) 0,4

LeafElement(内容)0,4

LeafElement(content) 0,4

LeafElement(内容)0,4

LeafElement(content) 0,4

LeafElement(内容)0,4

LeafElement(content) 0,4

LeafElement(组件)4,5

LeafElement(component) 4,5

LeafElement(内容)5,9

LeafElement(content) 5,9

LeafElement(内容)5,9

LeafElement(content) 5,9

LeafElement(内容)5,9

LeafElement(content) 5,9

LeafElement(内容)5,9

LeafElement(content) 5,9

LeafElement(组件)9,10

LeafElement(component) 9,10

看到 LeafElements 似乎有关于文档中哪个位置显示的内容的某种信息,我认为必须有可能获得该位置的实际内容。在搜索了另外半个小时后如何获取每个元素所代表的内容后,我放弃了并决定在这里发布我的问题,希望你们中的一些人可能知道如何实现这个目标!?

Seeing that the LeafElements that I got do seem to have some kind of information about what is displayed at which position in the Document, I figured that it must be possible to get the actual content at that position. After searching for another half hour how to get the content each of the elements represent, I gave up and decided to post my question here, hoping that some of you might know how to accomplish this!?

我见过这个问题有人试图通过 textPane.getComponents()访问组件,它返回一个组件数组,其中包含 JTextPane <中实际包含的组件的确切数量/ code>,但它们都是 javax.swing.text.ComponentView $ Invalidator 类型,这显然对我没用。也许我只是没有看到如何从这里正确继续,因为对我的符号的原始类型的强制转换不起作用。

I have seen this question where someone tries to access the components through textPane.getComponents(), which returns an array of components with the exact number of components actually contained in the JTextPane, but they are all of the type javax.swing.text.ComponentView$Invalidator, which is obviously of no use to me. Maybe I just don't see how to properly continue from here, because a cast to the original type of my symbol doesn't work.

tl; dr

如何获得 JComponent ,它位于<$ c $的文本内c> JTextPane ,它在文本窗格中的位置?

How do I get a JComponent, which is inside the text of a JTextPane, and its position from the text pane?

推荐答案

你可以遍历文本窗格 StyledDocument 查找代表组件或图标的元素,如下所示。

You can traverse the text pane's StyledDocument to find elements that represent components or icons, as shown below.


BranchElement(section) 0,7

BranchElement(paragraph) 0,7

LeafElement(content) 0,4

LeafElement(icon) 4,5

class javax.swing.plaf.IconUIResource
LeafElement(component) 5,6

class javax.swing.JLabel
LeafElement(content) 6,7

SSCCE:

/**
 * @see http://stackoverflow.com/a/15669307/230513
 * @see http://stackoverflow.com/questions/2883413
 */
public class DocumentParse {

    private static final String ELEM = AbstractDocument.ElementNameAttribute;
    private static final String ICON = StyleConstants.IconElementName;
    private static final String COMP = StyleConstants.ComponentElementName;

    public static void main(String args[]) throws Exception {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTextPane jtp = new JTextPane();
        StyledDocument doc = (StyledDocument) jtp.getDocument();
        SimpleAttributeSet normal = new SimpleAttributeSet();
        StyleConstants.setFontFamily(normal, "Serif");
        StyleConstants.setFontSize(normal, 72);
        StyleConstants.setForeground(normal, Color.blue);
        doc.insertString(doc.getLength(), "Test", normal);
        jtp.setSelectionStart(doc.getLength());
        jtp.insertIcon(UIManager.getIcon("OptionPane.warningIcon"));
        jtp.setSelectionStart(doc.getLength());
        jtp.insertComponent(new JLabel("Label"));
        jtp.setSelectionStart(doc.getLength());

        ElementIterator iterator = new ElementIterator(doc);
        Element element;
        while ((element = iterator.next()) != null) {
            System.out.println(element);
            AttributeSet as = element.getAttributes();
            if (as.containsAttribute(ELEM, ICON)) {
                System.out.println(StyleConstants.getIcon(as).getClass());
            }
            if (as.containsAttribute(ELEM, COMP)) {
                System.out.println(StyleConstants.getComponent(as).getClass());
            }
        }

        f.add(jtp);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}

这篇关于从JTextPane通过javax.swing.text.Element获取组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆