TextFieldViews 2
/*
Core SWING Advanced Programming
By Kim Topley
ISBN: 0 13 083292 8
Publisher: Prentice Hall
*/
import java.io.PrintStream;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import javax.swing.text.View;
public class TextFieldViews {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception evt) {}
JFrame f = new JFrame("Text Field View");
JTextField tf = new JTextField(32);
tf.setText("That's one small step for man...");
f.getContentPane().add(tf);
f.pack();
f.setVisible(true);
ViewDisplayer.displayViews(tf, System.out);
}
}
class ViewDisplayer {
public static void displayViews(JTextComponent comp, PrintStream out) {
View rootView = comp.getUI().getRootView(comp);
displayView(rootView, 0, comp.getDocument(), out);
}
public static void displayView(View view, int indent, Document doc,
PrintStream out) {
String name = view.getClass().getName();
for (int i = 0; i < indent; i++) {
out.print("\t");
}
int start = view.getStartOffset();
int end = view.getEndOffset();
out.println(name + "; offsets [" + start + ", " + end + "]");
int viewCount = view.getViewCount();
if (viewCount == 0) {
int length = Math.min(32, end - start);
try {
String txt = doc.getText(start, length);
for (int i = 0; i < indent + 1; i++) {
out.print("\t");
}
out.println("[" + txt + "]");
} catch (BadLocationException e) {
}
} else {
for (int i = 0; i < viewCount; i++) {
displayView(view.getView(i), indent + 1, doc, out);
}
}
}
}
Related examples in the same category