Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can traverse the text pane's <code>StyledDocument</code> to find elements that represent components or icons, as shown below.</p> <p><img src="https://i.stack.imgur.com/XLYbE.png" alt="image"></p> <pre> 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 </pre> <p>SSCCE:</p> <pre><code>/** * @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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload