Java Tips Weblog

  • Blog Stats

    • 1,277,226 hits
  • Categories

  • Archives

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.

rendered-component-tree

Get The Code

ComponentTreeCellRenderer.java

See Also

Component Tree Model

Related Reading

Java API: javax.swing.tree.DefaultTreeCellRenderer
Java API: javax.swing.JTree

About these ads

4 Responses to “Component Tree Cell Renderer”

  1. Torgil Zethson said

    I like the ComponentTree application. Very nice idea!

    Just a heads up about the renderer: when I run it with the new Nimbus look & feel (shipped with Java 6 update 10) the constructor crashes here:

    Color selBackground = UIManager.getColor(“Table.selectionBackground”);
    selColor = new Color(selBackground.getRGB());

    Apparently there’s no entry for “Table.selectionBackground” in Nimbus, so selBackground becomes null.

  2. Darryl Burke said

    Thanks Torgil. I’ve added a null check and default colors for any L&F that doesn’t provide its own. As I don’t have the Nimbus L&F, could you possibly confirm whether the problem is solved? Thank you.

    Darryl

  3. Torgil Zethson said

    The new version works fine on Nimbus.

    Cheers,
    - Torgil

  4. Darryl Burke said

    Thank you, Torgil.

    Darryl

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 96 other followers

%d bloggers like this: