At 09:27 PM 2/6/1999 +0530, sujay wrote:
>hi all,
> i was wondering if it is possible to assign different icons
> depending on some value
>to the various leaves in a tree.
>Example:
>[snip]
>
> what i want to do is i want all the odd numbers with one icon and all
> even number with
>another. can any body help me do this.
It's certainly possible.
The easiest way to change the way tree cells are rendered is, of
course, to subclass DefaultTreeCellRenderer. DTCR is itself a
JLabel which returns itself as a "rubber stamp" which can be used
over and over to paint all the nodes in the tree. In your subclass,
you would override the one method that all TreeCellRenderers must
implement, and do this, which is fairly cut and dried:
public Component getTreeCellRendererComponent (JTree tree,
Object value, boolean sel, boolean expanded, boolean leaf,
int row, boolean hasFocus)
{
super.getTreeCellRendererComponent (tree, value, sel, expanded,
leaf, row, hasFocus);
// do whatever you want here, mainly using methods defined on
// JLabel and JComponent, like setText(), setIcon(),
// setForeground(), etc.
return this;
}
That's all you have to do. In your case, you would set the icon to
_OddIcon_ or _EvenIcon_, say, depending on what you see in the
_value_ parameter.
_______________________________________________
Swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/swing