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
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, ?