// Retrieve the three icons
Icon leafIcon = new ImageIcon("leaf.gif");
Icon openIcon = new ImageIcon("open.gif");
Icon closedIcon = new ImageIcon("closed.gif");
// Create tree
JTree tree = new JTree();
// Update only one tree instance
DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer)tree.getCellRenderer();
renderer.setLeafIcon(leafIcon);
renderer.setClosedIcon(closedIcon);
renderer.setOpenIcon(openIcon);
// Remove the icons
renderer.setLeafIcon(null);
renderer.setClosedIcon(null);
renderer.setOpenIcon(null);
// Change defaults so that all new tree components will have new icons
UIManager.put("Tree.leafIcon", leafIcon);
UIManager.put("Tree.openIcon", openIcon);
UIManager.put("Tree.closedIcon", closedIcon);
// Create tree with new icons
tree = new JTree();
TREE <<< ein ICON
MENU1 <<< ein anderes ICON
A <<< ein anderes ICON
B <<< ein anderes ICON
C <<< ein anderes ICON
public final Component getTreeCellRendererComponent (final JTree tree,
final Object value, final boolean sel, final boolean expanded,
final boolean leaf, final int row, final boolean hasfocus)
{
super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row,
hasfocus);
// hier deinen Knotentyp einbauen
if (value instanceof DefaultMutableTreeNode) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
MeinObjekt obj = (MeinObjekt) node.getUserObject();
// hier den Vergleich auf deinen Knotentyp, ich habs mal als String-Vergleich gebaut
if (obj.getName.equals("TREE")) {
this.setLeaf("Icon1");
} else if (obj.getName.equals("MENU1")) {
this.setLeaf("Icon2");
} else if ... // beliebig viele weitere Typen
} else {
// default-Icon
this.setLeaf("default");
}
}
else {
System.err.println("Falscher Typ: " + value.getClass().toString());
}
return this;
}