Click to See Complete Forum and Search --> : TreeCellRenderer Doubt


cpraveenbabu
September 18th, 2000, 11:27 PM
Hi,
Im not getting different icons for different
files in a tree.
setLeafIcon() changes all files to the same color

My code is

for(int i=0;i<list.length;i++)
{
if (list[i].equals("receiver") )
{
renderer.setLeafIcon(new ImageIcon("green.gif")
);
> > > setCellRenderer(renderer);
> > > }
> > > else if (list[i].equals("transmitter"))
> > > {
> > > renderer.setLeafIcon(new
> > > ImageIcon("redfile.gif") );
> > > setCellRenderer(renderer);
> > > }
> > > else if (list[i].equals("recupdated"))
> > > {
> > > renderer.setLeafIcon(new
> > > ImageIcon("bluefile.gif") );
> > > setCellRenderer(renderer);
> > > }
> > > else if (list[i].equals("transupdated"))
> > > {
> > > renderer.setLeafIcon(new
> > > ImageIcon("yellowfile.gif") );
> > > setCellRenderer(renderer);
> > > }
> > > }
> > >
For each condition it checks, it changes
> all
> > > icons to the same
> > > color.So if the last file is "transupdated" all
> the
file icons change color
to yellowfile.gif
Try giving me a solution ..and if possible make changes in the particular code and send it

Best Regards,
Praveen.C

When going gets tough,
Tough gets going.

Phill
September 19th, 2000, 04:20 AM
Hi there
I think you have got the TreeCellRenderer for the
whole tree.
What you need is one for each leaf,(using the
getCellRenderer() method) then you
can change the particular one ie:
greenrenderer.setLeafIcon(new ImageIcon("green.gif")
);
> > > setCellRenderer(greenrenderer);
> > > }
else
redrenderer.setLeafIcon(new ImageIcon("red.gif")
);
> > > setCellRenderer(redrenderer);
> > > }

I hope this helps
Phill.

mjpell
September 19th, 2000, 02:25 PM
Here's an example, I didn't actually change the icons, but the hook is shown below.
Hope this helps.


/**
* Over written to set the tool tips. The tool tip is simply the value, which is
* the string which gets displayed. This is useful when the string is longer than the
* on-screen component is wide.
*/
public java.awt.Component getTreeCellRendererComponent(javax.swing.JTree tree, Object value, boolean sel,
boolean expanded, boolean leaf, int row, boolean hasFocus)
{
super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
if (true)
{
setToolTipText(value.toString());
}
else
{
setToolTipText(null); //no tool tip
}

//Optionally set the icons here
//if (leaf)
//{
//this.setIcon(getDefaultLeafIcon()); //specify which icon heres
//}
return this;
}