I’m new to Java and i have a JTree that I want to add some nodes then I want to make a custom renderer for the control to make each node has a different icon and as I came from a c# background I want to add Tag to each node to make it a criteria of choosing the icon, can anyone help me in that?
P.S: I don’t want to switch case over its string text because it may change from language to another
Here’s the code sample I used to add the nodes;
DefaultTreeModel model = (DefaultTreeModel) jTree1.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
for (int j = 0; j < 5; j++) {
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Main :" + j);
model.insertNodeInto(node1, root, root.getChildCount());
for (int i = 0; i < 4; i++) {
DefaultMutableTreeNode tmpnode = new DefaultMutableTreeNode(i);
model.insertNodeInto(tmpnode, node1, node1.getChildCount());
}
}
The constructor can obtain any object. Define your own NodeInfo class where you can define any desired fields. Overriding toString() of the class should produce simple representation (or you can write custom renderer).