Customizing leaves of a JTree
Posted on 1998-12-31
I am constructing a JTree using JDK1.2 on Windows NT and would like to customize the look of individuals terminal nodes (leaves). The application I'm developing compares two databases, represents their meta data in a tree format and visually identifies differences between them.
As an example, I want a node present in the one tree but not present in the other, to be have a different icon and color. I know that I can change all the leaf icons by doing:
UIManager.put("Tree.leafIcon", new ImageIcon("icon.gif"));
This doesn't cut it though... I want to be able to change the icon on a specific leaf, not all of them. I've looked for how to change the Font of the leaf, but I'm a little vague on this. Again, I want to set the font (rather, color of the font) on a particular leaf, not all of them.
The general nature of my code is that I have a JFrame, with a SplitPane (horizontal) with two ScrollPanes (one in each split) and a JTree in each ScrollPane. I've been creating node leaves with the following syntax:
node = new DefaultMutableTreeNode("name of leaf");
treeModel.insertNodeInto(node, parentNode, 0);
Just to be clear, here is a visual idea of what i'm after:
* different leaf <--different icon and font
Sample code would be great, or if you know that what I want to do can't be done, that is helpful too. Many thanks in advance!