有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java通过javax从JTextPane获取组件。摆动文本要素

我使用JTextPane来显示字符和符号,其中后者由自定义绘制的JComponents表示。例如,文本窗格可能显示如下内容: enter image description here 文本窗格是用户可编辑的,允许用户通过按钮在任何位置添加更多符号,并替换选定的文本。我通过JTextPane.insertComponent()方法来实现这一点。在应用程序中的某个时刻,我需要知道文本窗格中当前显示的内容,我指的不仅是输入的文本,还包括其中包含的确切组件

我在PositionsDocumentListeners管理文本窗格的内容时遇到了很多麻烦,但我一直在制造比我解决的问题更多的问题。这就是为什么我最终决定,我的问题可能是由于我的设计错误,所以我决定看看,如果我不能通过文本窗格访问我的组件

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

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

这给了我:

LeafElement(content) 0,4

LeafElement(content) 0,4

LeafElement(content) 0,4

LeafElement(content) 0,4

LeafElement(component) 4,5

LeafElement(content) 5,9

LeafElement(content) 5,9

LeafElement(content) 5,9

LeafElement(content) 5,9

LeafElement(component) 9,10

看到我得到的LeafElements似乎有一些关于在Document中的哪个位置显示什么的信息,我想一定有可能在那个位置获得实际内容。在又搜索了半个小时如何获取每个元素所代表的内容后,我放弃了,决定在这里发布我的问题,希望你们中的一些人可能知道如何实现这一点

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

tl;dr

如何从文本窗格中获取位于JTextPane文本内部的JComponent及其位置


共 (2) 个答案

  1. # 1 楼答案

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

    image

    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);
        }
    }
    
  2. # 2 楼答案

    从ComponentView中可以看到,原始组件是javax.swing.text.ComponentView$Invalidator的第一个(也是唯一的)子组件

    您可以获取无效器列表,并使用其子级访问插入的组件