Enumerating All the Views in a JTextComponent - Java Swing

Java examples for Swing:JTextComponent

Description

Enumerating All the Views in a JTextComponent

Demo Code


import javax.swing.JTextPane;
import javax.swing.text.JTextComponent;
import javax.swing.text.View;

public class Main {

  public void m() {
    JTextComponent textComp = new JTextPane();

    // Get the root view
    View v = textComp.getUI().getRootView(textComp);

    // Walk the view hierarchy
    walkView(v, 0);/* w  ww.jav  a  2 s  . c om*/
  }

  public void walkView(View view, int level) {
    // Process view...

    // Get number of children views
    int n = view.getViewCount();

    // Visit the children of this view
    for (int i = 0; i < n; i++) {
      walkView(view.getView(i), level + 1);
    }
  }
}

Related Tutorials