Changing icon in JTree like IDE...
Hi' i'm trying to search the answer in this forum, but i couldn't...
my answer is how to change the icon in JTree like JTree icon in any Java IDE...what i mean like this :
i already use this :
DefaultTreeCellRenderer render =new DefaultTreeCellRenderer();
render.setLeafIcon(new ImageIcon("images/java.png"));
render.setOpenIcon(new ImageIcon("images/General.gif"));
render.setClosedIcon(new ImageIcon("images/General.gif"));
render.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
But, all of this way is not what i want, if i'm just using this way, i only can change the icon in 3 type (leafIcon,OpenIcon,ClosedIcon) not like in any IDE (like Eclipse,or else..) that have 4 type icon or maybe more than 4 type...
in Eclipse every node in JTree have their own look..(ex: Package icon, Root Icon, Project icon, class icon, external library icon...)
How to implement that? i hope my answer are very clear to understand...
thanks a lot...
Hi,
You should override the class DefaultTreeCellRenderer.
protected class TreeRenderer extends DefaultTreeCellRenderer {
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
if (value instanceof PackageIcon) {
setIcon(packageIcon);
}
else if (value instanceof RootIcon) {
setIcon(rootIcon);
}
// etc...
return this;
}
}
Bye
Hi...thanks for the answer, but can u explain me more specific, cos i don't understand very well..i'm sorry...
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row, boolean hasFocus)
how to use it?
what is Object value? is that my Node? and then what about the boolean? i should set the boolean with what? true or false? and so int row?
thanks a lot...
hi, i'm using it like this, but not working..help..
class myTree extends JTree
{
...............
this.setCellRenderer(new TreeRenderer());
................
}
class TreeRenderer extends DefaultTreeCellRenderer
{
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row, boolean hasFocus)
{
if (value instanceof PackageIcon)
{
setIcon(PackageIcon);
}
....
return this;
}
}
but it doesn't work...how to use it? please help...this is makes me feel worst...