Get component from JTextPane via javax.swing.text.Element?

I use JTextPaneto display characters and characters where the latter are represented by a user schedule JComponents. For example, a text panel may display the following: The enter image description here text panel is editable by the user, and the user is allowed to add more characters using the button at any position and as a replacement for the selected text. I do this using a method JTextPane.insertComponent(). At some point in the application, I need to know what is currently displayed in the text panel, and this means not only the entered text, but also the exact components contained inside.

I overcame extensive problems with Positionsand DocumentListenersto manage the contents of my text panel, but I continued to cause more problems than I solved. That's why I finally decided that my problems were probably related to a design error on my part, so I decided to see if I could not get to my components through the text box.

Looking for documentation and source code AbstractDocumentand other related classes, I found an interface javax.swing.text.Element. Then I let my application output

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

who gave me:

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

JComponent, JTextPane, ?

+5
2

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);
    }
}
+10

( ) javax.swing.text.ComponentView$Invalidator, ComponentView.

.

+4

All Articles