Component Tree Cell Renderer
Posted by Darryl Burke on November 16, 2008
A previous posting, Component Tree Model, showed how to reproduce the hierarchy of a GUI in a JTree. The default node rendering in a JTree is the String returned from invoking toString(). For reviewing the nesting of components in a GUI, a visual rendering is infinitely more useful and appealing.
ComponentTreeCellRenderer provides this functionality and, when used in conjunction with a ComponentTreeModel, provides all you need to analyze the layout of a GUI in minute detail.
To see the ComponentTreeCellRenderer in action, simply add a line to the ComponentTree example supplied for ComponentTreeModel.
final JTree tree = new JTree(new ComponentTreeModel(root));
// add the next line
tree.setCellRenderer(new ComponentTreeCellRenderer(tree, 0.5));
This will render images of the components in the tree at half their real size.