ExtendableTreeCellRenderer.javaAPI DocApache Poi 3.0.14856Mon Jan 01 12:39:34 GMT 2007org.apache.poi.contrib.poibrowser


public class ExtendableTreeCellRenderer extends Object implements TreeCellRenderer

This is a {@link TreeCellRenderer} implementation which is able to render arbitrary objects. The {@link ExtendableTreeCellRenderer} does not do the rendering itself but instead dispatches to class-specific renderers. A class/renderer pair must be registered using the {@link #register} method. If a class has no registered renderer, the renderer of its closest superclass is used. Since the {@link ExtendableTreeCellRenderer} always has a default renderer for the {@link Object} class, rendering is always possible. The default {@link Object} renderer can be replaced by another renderer but it cannot be unregistered.

Rainer Klute <>
$Id: 489730 2006-12-22 19:18:16Z bayard $

Fields Summary
protected Map

Maps classes to renderers.

Constructors Summary
public ExtendableTreeCellRenderer()

        renderers = new HashMap();
        register(Object.class, new DefaultTreeCellRenderer()
                public Component getTreeCellRendererComponent
                    (JTree tree, Object value, boolean selected,
                     boolean expanded, boolean leaf, int row, boolean hasFocus)
                    final String s = value.toString();
                    final JLabel l = new JLabel(s + "  ");
                    if (selected)
                    return l;
Methods Summary
protected javax.swing.tree.TreeCellRendererfindRenderer(java.lang.Class c)

Find the renderer for the specified class.

        final TreeCellRenderer r = (TreeCellRenderer) renderers.get(c);
        if (r != null)
            /* The class has a renderer. */
            return r;

        /* The class has no renderer, try the superclass, if any. */
        final Class superclass = c.getSuperclass();
        if (superclass != null)
            return findRenderer(superclass);
            return null;
public java.awt.ComponentgetTreeCellRendererComponent(javax.swing.JTree tree, java.lang.Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus)

Renders an object in a tree cell depending of the object's class.


        final String NULL = "null";
        TreeCellRenderer r;
        Object userObject;
        if (value == null)
            userObject = NULL;
            userObject = ((DefaultMutableTreeNode) value).getUserObject();
            if (userObject == null)
                userObject = NULL;
        r = findRenderer(userObject.getClass());
        return r.getTreeCellRendererComponent
            (tree, value, selected, expanded, leaf, row,
public voidregister(java.lang.Class c, javax.swing.tree.TreeCellRenderer renderer)

Registers a renderer for a class.

        renderers.put(c, renderer);
public voidunregister(java.lang.Class c)

Unregisters a renderer for a class. The renderer for the {@link Object} class cannot be unregistered.

        if (c == Object.class)
            throw new IllegalArgumentException
                ("Renderer for Object cannot be unregistered.");
        renderers.put(c, null);