Listing the Attributes in a Style - Java Swing

Java examples for Swing:JTextPane

Description

Listing the Attributes in a Style

Demo Code

import java.util.Enumeration;

import javax.swing.JTextPane;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;

public class Main {
  public void main() {
    JTextPane textPane = new JTextPane();

    DefaultStyledDocument doc = (DefaultStyledDocument) textPane.getDocument();
    Enumeration e1 = doc.getStyleNames();
    while (e1.hasMoreElements()) {
      String styleName = (String) e1.nextElement();

      // Get style object
      Style style = doc.getStyle(styleName);

      // Get number of attributes
      int count = style.getAttributeCount();

      Enumeration e = style.getAttributeNames();
      while (e.hasMoreElements()) {
        Object o = e.nextElement();
        if (o instanceof String) {
          String attrName = (String) o;
          Object attrValue = style.getAttribute(attrName);
        } else if (o == StyleConstants.NameAttribute) {
          // Retrieve the style's name
          styleName = (String) style.getAttribute(o);
        } else if (o == StyleConstants.ResolveAttribute) {
          // Retrieve the style's parent
          Style parent = (Style) style.getAttribute(o);
        } else {//ww w  . java 2 s  .  co m
          // Retrieve the style constant name and value
          String attrName = o.toString();
          Object attrValue = style.getAttribute(o);
        }
      }

    }
  }
}

Related Tutorials