FileDocCategorySizeDatePackage
DefaultTreeCellRenderer.javaAPI DocJava SE 6 API17890Tue Jun 10 00:27:02 BST 2008javax.swing.tree

DefaultTreeCellRenderer

public class DefaultTreeCellRenderer extends JLabel implements TreeCellRenderer
Displays an entry in a tree. DefaultTreeCellRenderer is not opaque and unless you subclass paint you should not change this. See How to Use Trees in The Java Tutorial for examples of customizing node display using this class.

Implementation Note: This class overrides invalidate, validate, revalidate, repaint, and firePropertyChange solely to improve performance. If not overridden, these frequently called methods would execute code paths that are unnecessary for the default tree cell renderer. If you write your own renderer, take care to weigh the benefits and drawbacks of overriding these methods.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the java.beans package. Please see {@link java.beans.XMLEncoder}.

version
1.61 01/08/07
author
Rob Davis
author
Ray Ryan
author
Scott Violet

Fields Summary
private JTree
tree
Last tree the renderer was painted in.
protected boolean
selected
Is the value currently selected.
protected boolean
hasFocus
True if has focus.
private boolean
drawsFocusBorderAroundIcon
True if draws focus border around icon as well.
private boolean
drawDashedFocusIndicator
If true, a dashed line is drawn as the focus indicator.
private Color
treeBGColor
Background color of the tree.
private Color
focusBGColor
Color to draw the focus indicator in, determined from the background. color.
protected transient Icon
closedIcon
Icon used to show non-leaf nodes that aren't expanded.
protected transient Icon
leafIcon
Icon used to show leaf nodes.
protected transient Icon
openIcon
Icon used to show non-leaf nodes that are expanded.
protected Color
textSelectionColor
Color to use for the foreground for selected nodes.
protected Color
textNonSelectionColor
Color to use for the foreground for non-selected nodes.
protected Color
backgroundSelectionColor
Color to use for the background when a node is selected.
protected Color
backgroundNonSelectionColor
Color to use for the background when the node isn't selected.
protected Color
borderSelectionColor
Color to use for the focus indicator when the node has focus.
private boolean
isDropCell
Constructors Summary
public DefaultTreeCellRenderer()
Returns a new instance of DefaultTreeCellRenderer. Alignment is set to left aligned. Icons and text color are determined from the UIManager.

	setLeafIcon(UIManager.getIcon("Tree.leafIcon"));
	setClosedIcon(UIManager.getIcon("Tree.closedIcon"));
	setOpenIcon(UIManager.getIcon("Tree.openIcon"));

	setTextSelectionColor(UIManager.getColor("Tree.selectionForeground"));
	setTextNonSelectionColor(UIManager.getColor("Tree.textForeground"));
	setBackgroundSelectionColor(UIManager.getColor("Tree.selectionBackground"));
	setBackgroundNonSelectionColor(UIManager.getColor("Tree.textBackground"));
	setBorderSelectionColor(UIManager.getColor("Tree.selectionBorderColor"));
	Object value = UIManager.get("Tree.drawsFocusBorderAroundIcon");
	drawsFocusBorderAroundIcon = (value != null && ((Boolean)value).
				      booleanValue());
	value = UIManager.get("Tree.drawDashedFocusIndicator");
	drawDashedFocusIndicator = (value != null && ((Boolean)value).
				    booleanValue());
    
Methods Summary
protected voidfirePropertyChange(java.lang.String propertyName, java.lang.Object oldValue, java.lang.Object newValue)
Overridden for performance reasons. See the Implementation Note for more information.

	
	// Strings get interned...
	if (propertyName == "text"
                || ((propertyName == "font" || propertyName == "foreground")
                    && oldValue != newValue
                    && getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey) != null)) {

	    super.firePropertyChange(propertyName, oldValue, newValue);
        }
    
public voidfirePropertyChange(java.lang.String propertyName, byte oldValue, byte newValue)
Overridden for performance reasons. See the Implementation Note for more information.

public voidfirePropertyChange(java.lang.String propertyName, char oldValue, char newValue)
Overridden for performance reasons. See the Implementation Note for more information.

public voidfirePropertyChange(java.lang.String propertyName, short oldValue, short newValue)
Overridden for performance reasons. See the Implementation Note for more information.

public voidfirePropertyChange(java.lang.String propertyName, int oldValue, int newValue)
Overridden for performance reasons. See the Implementation Note for more information.

public voidfirePropertyChange(java.lang.String propertyName, long oldValue, long newValue)
Overridden for performance reasons. See the Implementation Note for more information.

public voidfirePropertyChange(java.lang.String propertyName, float oldValue, float newValue)
Overridden for performance reasons. See the Implementation Note for more information.

public voidfirePropertyChange(java.lang.String propertyName, double oldValue, double newValue)
Overridden for performance reasons. See the Implementation Note for more information.

public voidfirePropertyChange(java.lang.String propertyName, boolean oldValue, boolean newValue)
Overridden for performance reasons. See the Implementation Note for more information.

public java.awt.ColorgetBackgroundNonSelectionColor()
Returns the background color to be used for non selected nodes.

	return backgroundNonSelectionColor;
    
public java.awt.ColorgetBackgroundSelectionColor()
Returns the color to use for the background if node is selected.

	return backgroundSelectionColor;
    
public java.awt.ColorgetBorderSelectionColor()
Returns the color the border is drawn.

	return borderSelectionColor;
    
public javax.swing.IcongetClosedIcon()
Returns the icon used to represent non-leaf nodes that are not expanded.

	return closedIcon;
    
public javax.swing.IcongetDefaultClosedIcon()
Returns the default icon, for the current laf, that is used to represent non-leaf nodes that are not expanded.

	return UIManager.getIcon("Tree.closedIcon");
    
public javax.swing.IcongetDefaultLeafIcon()
Returns the default icon, for the current laf, that is used to represent leaf nodes.

	return UIManager.getIcon("Tree.leafIcon");
    
public javax.swing.IcongetDefaultOpenIcon()
Returns the default icon, for the current laf, that is used to represent non-leaf nodes that are expanded.

	return UIManager.getIcon("Tree.openIcon");
    
public java.awt.FontgetFont()
Gets the font of this component.

return
this component's font; if a font has not been set for this component, the font of its parent is returned

        Font font = super.getFont();

        if (font == null && tree != null) {
            // Strive to return a non-null value, otherwise the html support
            // will typically pick up the wrong font in certain situations.
            font = tree.getFont();
        }
        return font;
    
private intgetLabelStart()

	Icon currentI = getIcon();
	if(currentI != null && getText() != null) {
	    return currentI.getIconWidth() + Math.max(0, getIconTextGap() - 1);
	}
	return 0;
    
public javax.swing.IcongetLeafIcon()
Returns the icon used to represent leaf nodes.

	return leafIcon;
    
public javax.swing.IcongetOpenIcon()
Returns the icon used to represent non-leaf nodes that are expanded.

	return openIcon;
    
public java.awt.DimensiongetPreferredSize()
Overrides JComponent.getPreferredSize to return slightly wider preferred size value.

	Dimension        retDimension = super.getPreferredSize();

	if(retDimension != null)
	    retDimension = new Dimension(retDimension.width + 3,
					 retDimension.height);
	return retDimension;
    
public java.awt.ColorgetTextNonSelectionColor()
Returns the color the text is drawn with when the node isn't selected.

	return textNonSelectionColor;
    
public java.awt.ColorgetTextSelectionColor()
Returns the color the text is drawn with when the node is selected.

	return textSelectionColor;
    
public java.awt.ComponentgetTreeCellRendererComponent(javax.swing.JTree tree, java.lang.Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus)
Configures the renderer based on the passed in components. The value is set from messaging the tree with convertValueToText, which ultimately invokes toString on value. The foreground color is set based on the selection and the icon is set based on the leaf and expanded parameters.

	String         stringValue = tree.convertValueToText(value, sel,
					  expanded, leaf, row, hasFocus);

        this.tree = tree;
	this.hasFocus = hasFocus;
	setText(stringValue);

        Color fg = null;
        isDropCell = false;

        JTree.DropLocation dropLocation = tree.getDropLocation();
        if (dropLocation != null
                && dropLocation.getChildIndex() == -1
                && tree.getRowForPath(dropLocation.getPath()) == row) {

            Color col = UIManager.getColor("Tree.dropCellForeground");
            if (col != null) {
                fg = col;
            } else {
                fg = getTextSelectionColor();
            }

            isDropCell = true;
        } else if (sel) {
            fg = getTextSelectionColor();
        } else {
            fg = getTextNonSelectionColor();
        }

        setForeground(fg);

	// There needs to be a way to specify disabled icons.
	if (!tree.isEnabled()) {
	    setEnabled(false);
	    if (leaf) {
		setDisabledIcon(getLeafIcon());
	    } else if (expanded) {
		setDisabledIcon(getOpenIcon());
	    } else {
		setDisabledIcon(getClosedIcon());
	    }
	}
	else {
	    setEnabled(true);
	    if (leaf) {
		setIcon(getLeafIcon());
	    } else if (expanded) {
		setIcon(getOpenIcon());
	    } else {
		setIcon(getClosedIcon());
	    }
	}
        setComponentOrientation(tree.getComponentOrientation());
	    
	selected = sel;

	return this;
    
public voidinvalidate()
Overridden for performance reasons. See the Implementation Note for more information.

since
1.5

public voidpaint(java.awt.Graphics g)
Paints the value. The background is filled based on selected.

	Color bColor;

        if (isDropCell) {
            bColor = UIManager.getColor("Tree.dropCellBackground");
            if (bColor == null) {
                bColor = getBackgroundSelectionColor();
            }
        } else if (selected) {
            bColor = getBackgroundSelectionColor();
	} else {
	    bColor = getBackgroundNonSelectionColor();
            if (bColor == null) {
                bColor = getBackground();
            }
	}

	int imageOffset = -1;
	if(bColor != null) {
	    Icon currentI = getIcon();

	    imageOffset = getLabelStart();
	    g.setColor(bColor);
	    if(getComponentOrientation().isLeftToRight()) {
	        g.fillRect(imageOffset, 0, getWidth() - imageOffset,
			   getHeight());
	    } else {
	        g.fillRect(0, 0, getWidth() - imageOffset,
			   getHeight());
	    }
	}

	if (hasFocus) {
	    if (drawsFocusBorderAroundIcon) {
		imageOffset = 0;
	    }
	    else if (imageOffset == -1) {
		imageOffset = getLabelStart();
	    }
	    if(getComponentOrientation().isLeftToRight()) {
		paintFocus(g, imageOffset, 0, getWidth() - imageOffset,
			   getHeight(), bColor);
	    } else {
		paintFocus(g, 0, 0, getWidth() - imageOffset, getHeight(), bColor);
	    }
	}
	super.paint(g);
    
private voidpaintFocus(java.awt.Graphics g, int x, int y, int w, int h, java.awt.Color notColor)

	Color       bsColor = getBorderSelectionColor();

	if (bsColor != null && (selected || !drawDashedFocusIndicator)) {
	    g.setColor(bsColor);
	    g.drawRect(x, y, w - 1, h - 1);
	}
        if (drawDashedFocusIndicator && notColor != null) {
	    if (treeBGColor != notColor) {
                treeBGColor = notColor;
                focusBGColor = new Color(~notColor.getRGB());
	    }
	    g.setColor(focusBGColor);
	    BasicGraphicsUtils.drawDashedRect(g, x, y, w, h);
	}
    
public voidrepaint(long tm, int x, int y, int width, int height)
Overridden for performance reasons. See the Implementation Note for more information.

public voidrepaint(java.awt.Rectangle r)
Overridden for performance reasons. See the Implementation Note for more information.

public voidrepaint()
Overridden for performance reasons. See the Implementation Note for more information.

since
1.5

public voidrevalidate()
Overridden for performance reasons. See the Implementation Note for more information.

public voidsetBackground(java.awt.Color color)
Subclassed to map ColorUIResources to null. If color is null, or a ColorUIResource, this has the effect of letting the background color of the JTree show through. On the other hand, if color is non-null, and not a ColorUIResource, the background becomes color.

	if(color instanceof ColorUIResource)
	    color = null;
	super.setBackground(color);
    
public voidsetBackgroundNonSelectionColor(java.awt.Color newColor)
Sets the background color to be used for non selected nodes.

	backgroundNonSelectionColor = newColor;
    
public voidsetBackgroundSelectionColor(java.awt.Color newColor)
Sets the color to use for the background if node is selected.

	backgroundSelectionColor = newColor;
    
public voidsetBorderSelectionColor(java.awt.Color newColor)
Sets the color to use for the border.

	borderSelectionColor = newColor;
    
public voidsetClosedIcon(javax.swing.Icon newIcon)
Sets the icon used to represent non-leaf nodes that are not expanded.

	closedIcon = newIcon;
    
public voidsetFont(java.awt.Font font)
Subclassed to map FontUIResources to null. If font is null, or a FontUIResource, this has the effect of letting the font of the JTree show through. On the other hand, if font is non-null, and not a FontUIResource, the font becomes font.

	if(font instanceof FontUIResource)
	    font = null;
	super.setFont(font);
    
public voidsetLeafIcon(javax.swing.Icon newIcon)
Sets the icon used to represent leaf nodes.

	leafIcon = newIcon;
    
public voidsetOpenIcon(javax.swing.Icon newIcon)
Sets the icon used to represent non-leaf nodes that are expanded.

	openIcon = newIcon;
    
public voidsetTextNonSelectionColor(java.awt.Color newColor)
Sets the color the text is drawn with when the node isn't selected.

	textNonSelectionColor = newColor;
    
public voidsetTextSelectionColor(java.awt.Color newColor)
Sets the color the text is drawn with when the node is selected.

	textSelectionColor = newColor;
    
public voidvalidate()
Overridden for performance reasons. See the Implementation Note for more information.