APIdia Launch Partner
Top Description Fields Constructors Methods
javax.swing.tree

public Class DefaultTreeCellRenderer

extends JLabel
implements TreeCellRenderer
Class Inheritance
All Implemented Interfaces
javax.swing.tree.TreeCellRenderer
Known Direct Subclasses
com.sun.java.swing.plaf.motif.MotifTreeCellRenderer, javax.swing.plaf.synth.SynthTreeUI.SynthTreeCellRenderer
Annotations
@SuppressWarnings:serial
Imports
java.awt.Color, .Component, .Dimension, .Font, .Graphics, .Insets, .Rectangle, javax.swing.plaf.BorderUIResource.EmptyBorderUIResource, .ColorUIResource, .FontUIResource, .UIResource, javax.swing.plaf.basic.BasicGraphicsUtils, javax.swing.Icon, .JLabel, .JTree, .LookAndFeel, .UIManager, javax.swing.border.EmptyBorder, sun.swing.DefaultLookup, .SwingUtilities2

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.

The set of icons and colors used by DefaultTreeCellRenderer can be configured using the various setter methods. The value for each property is initialized from the defaults table. When the look and feel changes (updateUI is invoked), any properties that have a value of type UIResource are refreshed from the defaults table. The following table lists the mapping between DefaultTreeCellRenderer property and defaults table key:

Properties
Property Key
"leafIcon" "Tree.leafIcon"
"closedIcon" "Tree.closedIcon"
"openIcon" "Tree.openIcon"
"textSelectionColor" "Tree.selectionForeground"
"textNonSelectionColor" "Tree.textForeground"
"backgroundSelectionColor" "Tree.selectionBackground"
"backgroundNonSelectionColor" "Tree.textBackground"
"borderSelectionColor" "Tree.selectionBorderColor"

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 JavaBeans has been added to the java.beans package. Please see java.beans.XMLEncoder.

Authors
Rob Davis, Ray Ryan, Scott Violet

Field Summary

Modifier and TypeField and Description
protected Color
backgroundNonSelectionColor

Color to use for the background when the node isn't selected.

protected Color
backgroundSelectionColor

Color to use for the background when a node is selected.

protected Color
borderSelectionColor

Color to use for the focus indicator when the node has focus.

protected transient Icon
closedIcon

Icon used to show non-leaf nodes that aren't expanded.

private boolean
drawDashedFocusIndicator

If true, a dashed line is drawn as the focus indicator.

private boolean
drawsFocusBorderAroundIcon

True if draws focus border around icon as well.

private boolean
private Color
focusBGColor

Color to draw the focus indicator in, determined from the background.

protected boolean
hasFocus

True if has focus.

private boolean
inited

Set to true after the constructor has run.

private boolean
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 boolean
selected

Is the value currently selected.

protected Color
textNonSelectionColor

Color to use for the foreground for non-selected nodes.

protected Color
textSelectionColor

Color to use for the foreground for selected nodes.

private JTree
tree

Last tree the renderer was painted in.

private Color
treeBGColor

Background color of the tree.

Inherited from javax.swing.JLabel:
labelFor

Constructor Summary

AccessConstructor and Description
public
DefaultTreeCellRenderer()

Creates a DefaultTreeCellRenderer.

Method Summary

Modifier and TypeMethod and Description
protected void
firePropertyChange(String propertyName, Object oldValue, Object newValue)

Overrides java.awt.Component.firePropertyChange.

Overridden for performance reasons.

public void
firePropertyChange(String propertyName, byte oldValue, byte newValue)

Overrides java.awt.Component.firePropertyChange.

Overridden for performance reasons.

public void
firePropertyChange(String propertyName, char oldValue, char newValue)

Overrides javax.swing.JComponent.firePropertyChange.

Overridden for performance reasons.

public void
firePropertyChange(String propertyName, short oldValue, short newValue)

Overrides java.awt.Component.firePropertyChange.

Overridden for performance reasons.

public void
firePropertyChange(String propertyName, int oldValue, int newValue)

Overrides javax.swing.JComponent.firePropertyChange.

Overridden for performance reasons.

public void
firePropertyChange(String propertyName, long oldValue, long newValue)

Overrides java.awt.Component.firePropertyChange.

Overridden for performance reasons.

public void
firePropertyChange(String propertyName, float oldValue, float newValue)

Overrides java.awt.Component.firePropertyChange.

Overridden for performance reasons.

public void
firePropertyChange(String propertyName, double oldValue, double newValue)

Overrides java.awt.Component.firePropertyChange.

Overridden for performance reasons.

public void
firePropertyChange(String propertyName, boolean oldValue, boolean newValue)

Overrides javax.swing.JComponent.firePropertyChange.

Overridden for performance reasons.

public Color

Returns:

the background color to be used for non selected nodes.
getBackgroundNonSelectionColor
()

Returns the background color to be used for non selected nodes.

public Color

Returns:

the color to use for the background if node is selected
getBackgroundSelectionColor
()

Returns the color to use for the background if node is selected.

public Color

Returns:

the color the border is drawn
getBorderSelectionColor
()

Returns the color the border is drawn.

public Icon

Returns:

the icon used to represent non-leaf nodes that are not expanded
getClosedIcon
()

Returns the icon used to represent non-leaf nodes that are not expanded.

public Icon

Returns:

the default icon, for the current laf, that is used to represent non-leaf nodes that are not expanded.
getDefaultClosedIcon
()

Returns the default icon, for the current laf, that is used to represent non-leaf nodes that are not expanded.

public Icon

Returns:

the default icon, for the current laf, that is used to represent leaf nodes.
getDefaultLeafIcon
()

Returns the default icon, for the current laf, that is used to represent leaf nodes.

public Icon

Returns:

the default icon, for the current laf, that is used to represent non-leaf nodes that are expanded.
getDefaultOpenIcon
()

Returns the default icon, for the current laf, that is used to represent non-leaf nodes that are expanded.

public Font

Returns:

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

Overrides java.awt.Component.getFont.

Implements java.awt.MenuContainer.getFont.

Gets the font of this component.

private int
public Icon

Returns:

the icon used to represent leaf nodes
getLeafIcon
()

Returns the icon used to represent leaf nodes.

public Icon

Returns:

the icon used to represent non-leaf nodes that are expanded
getOpenIcon
()

Returns the icon used to represent non-leaf nodes that are expanded.

public Dimension
getPreferredSize()

Overrides javax.swing.JComponent.getPreferredSize.

Overrides JComponent.getPreferredSize to return slightly wider preferred size value.

public Color

Returns:

the color the text is drawn with when the node isn't selected.
getTextNonSelectionColor
()

Returns the color the text is drawn with when the node isn't selected.

public Color

Returns:

the color the text is drawn with when the node is selected
getTextSelectionColor
()

Returns the color the text is drawn with when the node is selected.

public Component
getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus)

Implements javax.swing.tree.TreeCellRenderer.getTreeCellRendererComponent.

Configures the renderer based on the passed in components.

public void
invalidate()

Overrides java.awt.Container.invalidate.

Overridden for performance reasons.

public void
paint(Graphics g)

Overrides javax.swing.JComponent.paint.

Paints the value.

private void
paintFocus(Graphics g, int x, int y, int w, int h, Color notColor)

public void
repaint(long tm, int x, int y, int width, int height)

Overrides javax.swing.JComponent.repaint.

Overridden for performance reasons.

public void
repaint(Rectangle r)

Overrides javax.swing.JComponent.repaint.

Overridden for performance reasons.

public void
repaint()

Overrides java.awt.Component.repaint.

Overridden for performance reasons.

public void
revalidate()

Overrides javax.swing.JComponent.revalidate.

Overridden for performance reasons.

public void
setBackground(Color color)

Overrides javax.swing.JComponent.setBackground.

Subclassed to map ColorUIResources to null.

public void
setBackgroundNonSelectionColor(Color
color to be used for the background for non selected nodes
newColor
)

Sets the background color to be used for non selected nodes.

public void
setBackgroundSelectionColor(Color
to be used for the background if the node is selected
newColor
)

Sets the color to use for the background if node is selected.

public void
setBorderSelectionColor(Color
color to be used for the border
newColor
)

Sets the color to use for the border.

public void
setClosedIcon(Icon
the icon to be used for not expanded non-leaf nodes
newIcon
)

Sets the icon used to represent non-leaf nodes that are not expanded.

public void
setFont(Font font)

Overrides javax.swing.JComponent.setFont.

Subclassed to map FontUIResources to null.

public void
setLeafIcon(Icon
icon to be used for leaf nodes
newIcon
)

Sets the icon used to represent leaf nodes.

public void
setOpenIcon(Icon
the icon to be used for expanded non-leaf nodes
newIcon
)

Sets the icon used to represent non-leaf nodes that are expanded.

public void
setTextNonSelectionColor(Color
color to be used for text when the node isn't selected
newColor
)

Sets the color the text is drawn with when the node isn't selected.

public void
setTextSelectionColor(Color
color to be used for text when the node is selected
newColor
)

Sets the color the text is drawn with when the node is selected.

public void
updateUI()

Overrides javax.swing.JLabel.updateUI.

Resets the UI property to a value from the current look and feel.

public void
validate()

Overrides java.awt.Container.validate.

Overridden for performance reasons.

Inherited from javax.swing.JLabel:
checkHorizontalKeycheckVerticalKeygetAccessibleContextgetDisabledIcongetDisplayedMnemonicgetDisplayedMnemonicIndexgetHorizontalAlignmentgetHorizontalTextPositiongetIcongetIconTextGapgetLabelForgetTextgetUIgetUIClassIDgetVerticalAlignmentgetVerticalTextPositionimageUpdateparamStringsetDisabledIconsetDisplayedMnemonicsetDisplayedMnemonicsetDisplayedMnemonicIndexsetHorizontalAlignmentsetHorizontalTextPositionsetIconsetIconTextGapsetLabelForsetTextsetUIsetVerticalAlignmentsetVerticalTextPosition

Field Detail

backgroundNonSelectionColorback to summary
protected Color backgroundNonSelectionColor

Color to use for the background when the node isn't selected.

backgroundSelectionColorback to summary
protected Color backgroundSelectionColor

Color to use for the background when a node is selected.

borderSelectionColorback to summary
protected Color borderSelectionColor

Color to use for the focus indicator when the node has focus.

closedIconback to summary
protected transient Icon closedIcon

Icon used to show non-leaf nodes that aren't expanded.

drawDashedFocusIndicatorback to summary
private boolean drawDashedFocusIndicator

If true, a dashed line is drawn as the focus indicator.

drawsFocusBorderAroundIconback to summary
private boolean drawsFocusBorderAroundIcon

True if draws focus border around icon as well.

fillBackgroundback to summary
private boolean fillBackground
focusBGColorback to summary
private Color focusBGColor

Color to draw the focus indicator in, determined from the background. color.

hasFocusback to summary
protected boolean hasFocus

True if has focus.

initedback to summary
private boolean inited

Set to true after the constructor has run.

isDropCellback to summary
private boolean isDropCell
leafIconback to summary
protected transient Icon leafIcon

Icon used to show leaf nodes.

openIconback to summary
protected transient Icon openIcon

Icon used to show non-leaf nodes that are expanded.

selectedback to summary
protected boolean selected

Is the value currently selected.

textNonSelectionColorback to summary
protected Color textNonSelectionColor

Color to use for the foreground for non-selected nodes.

textSelectionColorback to summary
protected Color textSelectionColor

Color to use for the foreground for selected nodes.

treeback to summary
private JTree tree

Last tree the renderer was painted in.

treeBGColorback to summary
private Color treeBGColor

Background color of the tree.

Constructor Detail

DefaultTreeCellRendererback to summary
public DefaultTreeCellRenderer()

Creates a DefaultTreeCellRenderer. Icons and text color are determined from the UIManager.

Method Detail

firePropertyChangeback to summary
protected void firePropertyChange(String propertyName, Object oldValue, Object newValue)

Overrides java.awt.Component.firePropertyChange.

Overridden for performance reasons. See the Implementation Note for more information.

firePropertyChangeback to summary
public void firePropertyChange(String propertyName, byte oldValue, byte newValue)

Overrides java.awt.Component.firePropertyChange.

Overridden for performance reasons. See the Implementation Note for more information.

firePropertyChangeback to summary
public void firePropertyChange(String propertyName, char oldValue, char newValue)

Overrides javax.swing.JComponent.firePropertyChange.

Overridden for performance reasons. See the Implementation Note for more information.

firePropertyChangeback to summary
public void firePropertyChange(String propertyName, short oldValue, short newValue)

Overrides java.awt.Component.firePropertyChange.

Overridden for performance reasons. See the Implementation Note for more information.

firePropertyChangeback to summary
public void firePropertyChange(String propertyName, int oldValue, int newValue)

Overrides javax.swing.JComponent.firePropertyChange.

Overridden for performance reasons. See the Implementation Note for more information.

firePropertyChangeback to summary
public void firePropertyChange(String propertyName, long oldValue, long newValue)

Overrides java.awt.Component.firePropertyChange.

Overridden for performance reasons. See the Implementation Note for more information.

firePropertyChangeback to summary
public void firePropertyChange(String propertyName, float oldValue, float newValue)

Overrides java.awt.Component.firePropertyChange.

Overridden for performance reasons. See the Implementation Note for more information.

firePropertyChangeback to summary
public void firePropertyChange(String propertyName, double oldValue, double newValue)

Overrides java.awt.Component.firePropertyChange.

Overridden for performance reasons. See the Implementation Note for more information.

firePropertyChangeback to summary
public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue)

Overrides javax.swing.JComponent.firePropertyChange.

Overridden for performance reasons. See the Implementation Note for more information.

getBackgroundNonSelectionColorback to summary
public Color getBackgroundNonSelectionColor()

Returns the background color to be used for non selected nodes.

Returns:Color

the background color to be used for non selected nodes.

getBackgroundSelectionColorback to summary
public Color getBackgroundSelectionColor()

Returns the color to use for the background if node is selected.

Returns:Color

the color to use for the background if node is selected

getBorderSelectionColorback to summary
public Color getBorderSelectionColor()

Returns the color the border is drawn.

Returns:Color

the color the border is drawn

getClosedIconback to summary
public Icon getClosedIcon()

Returns the icon used to represent non-leaf nodes that are not expanded.

Returns:Icon

the icon used to represent non-leaf nodes that are not expanded

getDefaultClosedIconback to summary
public Icon getDefaultClosedIcon()

Returns the default icon, for the current laf, that is used to represent non-leaf nodes that are not expanded.

Returns:Icon

the default icon, for the current laf, that is used to represent non-leaf nodes that are not expanded.

getDefaultLeafIconback to summary
public Icon getDefaultLeafIcon()

Returns the default icon, for the current laf, that is used to represent leaf nodes.

Returns:Icon

the default icon, for the current laf, that is used to represent leaf nodes.

getDefaultOpenIconback to summary
public Icon getDefaultOpenIcon()

Returns the default icon, for the current laf, that is used to represent non-leaf nodes that are expanded.

Returns:Icon

the default icon, for the current laf, that is used to represent non-leaf nodes that are expanded.

getFontback to summary
public Font getFont()

Overrides java.awt.Component.getFont.

Implements java.awt.MenuContainer.getFont.

Gets the font of this component.

Returns:Font

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

getLabelStartback to summary
private int getLabelStart()
getLeafIconback to summary
public Icon getLeafIcon()

Returns the icon used to represent leaf nodes.

Returns:Icon

the icon used to represent leaf nodes

getOpenIconback to summary
public Icon getOpenIcon()

Returns the icon used to represent non-leaf nodes that are expanded.

Returns:Icon

the icon used to represent non-leaf nodes that are expanded

getPreferredSizeback to summary
public Dimension getPreferredSize()

Overrides javax.swing.JComponent.getPreferredSize.

Overrides JComponent.getPreferredSize to return slightly wider preferred size value.

getTextNonSelectionColorback to summary
public Color getTextNonSelectionColor()

Returns the color the text is drawn with when the node isn't selected.

Returns:Color

the color the text is drawn with when the node isn't selected.

getTextSelectionColorback to summary
public Color getTextSelectionColor()

Returns the color the text is drawn with when the node is selected.

Returns:Color

the color the text is drawn with when the node is selected

getTreeCellRendererComponentback to summary
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus)

Implements javax.swing.tree.TreeCellRenderer.getTreeCellRendererComponent.

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.

invalidateback to summary
public void invalidate()

Overrides java.awt.Container.invalidate.

Overridden for performance reasons. See the Implementation Note for more information.

Since
1.5
paintback to summary
public void paint(Graphics g)

Overrides javax.swing.JComponent.paint.

Paints the value. The background is filled based on selected.

paintFocusback to summary
private void paintFocus(Graphics g, int x, int y, int w, int h, Color notColor)
repaintback to summary
public void repaint(long tm, int x, int y, int width, int height)

Overrides javax.swing.JComponent.repaint.

Overridden for performance reasons. See the Implementation Note for more information.

repaintback to summary
public void repaint(Rectangle r)

Overrides javax.swing.JComponent.repaint.

Overridden for performance reasons. See the Implementation Note for more information.

repaintback to summary
public void repaint()

Overrides java.awt.Component.repaint.

Overridden for performance reasons. See the Implementation Note for more information.

Since
1.5
revalidateback to summary
public void revalidate()

Overrides javax.swing.JComponent.revalidate.

Overridden for performance reasons. See the Implementation Note for more information.

setBackgroundback to summary
public void setBackground(Color color)

Overrides javax.swing.JComponent.setBackground.

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.

setBackgroundNonSelectionColorback to summary
public void setBackgroundNonSelectionColor(Color newColor)

Sets the background color to be used for non selected nodes.

Parameters
newColor:Color

color to be used for the background for non selected nodes

setBackgroundSelectionColorback to summary
public void setBackgroundSelectionColor(Color newColor)

Sets the color to use for the background if node is selected.

Parameters
newColor:Color

to be used for the background if the node is selected

setBorderSelectionColorback to summary
public void setBorderSelectionColor(Color newColor)

Sets the color to use for the border.

Parameters
newColor:Color

color to be used for the border

setClosedIconback to summary
public void setClosedIcon(Icon newIcon)

Sets the icon used to represent non-leaf nodes that are not expanded.

Parameters
newIcon:Icon

the icon to be used for not expanded non-leaf nodes

setFontback to summary
public void setFont(Font font)

Overrides javax.swing.JComponent.setFont.

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.

setLeafIconback to summary
public void setLeafIcon(Icon newIcon)

Sets the icon used to represent leaf nodes.

Parameters
newIcon:Icon

icon to be used for leaf nodes

setOpenIconback to summary
public void setOpenIcon(Icon newIcon)

Sets the icon used to represent non-leaf nodes that are expanded.

Parameters
newIcon:Icon

the icon to be used for expanded non-leaf nodes

setTextNonSelectionColorback to summary
public void setTextNonSelectionColor(Color newColor)

Sets the color the text is drawn with when the node isn't selected.

Parameters
newColor:Color

color to be used for text when the node isn't selected

setTextSelectionColorback to summary
public void setTextSelectionColor(Color newColor)

Sets the color the text is drawn with when the node is selected.

Parameters
newColor:Color

color to be used for text when the node is selected

updateUIback to summary
public void updateUI()

Overrides javax.swing.JLabel.updateUI.

Doc from javax.swing.JLabel.updateUI.

Resets the UI property to a value from the current look and feel.

Since
1.7
validateback to summary
public void validate()

Overrides java.awt.Container.validate.

Overridden for performance reasons. See the Implementation Note for more information.