following multiple tutorials and question like this or this I am trying to create a Jtree dom which should be able to have different Icons depending on the properties beeing set in the application, but i am not able to find a way to set the icon properly since it doesn´t display correct after beeing set.
I did create my own class XML_JTRE_ELEM
which has JTree as super class and also implements the TreeExpansionListener
The overwritten Method for the expansion looks like this
public void treeExpanded(TreeExpansionEvent event) {
TreePath path = event.getPath();
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
XML_JTREE_ELEM elem = (XML_JTREE_ELEM)node.getUserObject(); // Get the current JTREE
for(String key : elem.getSubTrees().keySet()) {
// Set the icon dynamcally for each subtree item
elem.getSubTrees().get(key).setIcon();
}
}
the setIcon Method simply sets a new CellRenderer for each subtree
public void setIcon() {
DefaultTreeCellRenderer renderer = new TagRenderer(ImageList.sum);
this.setCellRenderer(renderer);
}
The CellRenderer itself looks like this:
private class TagRenderer extends DefaultTreeCellRenderer{
private static final long serialVersionUID = 1L;
private ImageIcon _icon;
public TagRenderer(ImageIcon icon) {
this._icon = icon;
}
@Override
public Component getTreeCellRendererComponent(JTree tree,
Object value, boolean selected, boolean expanded,
boolean isLeaf, int row, boolean focused) {
setIcon(_icon);
System.out.Println(_icon.toString());
return this;
}
}
When i am printing out the _icon i am getting the expected result file:/C:/java_ws/project/bin/resources/Sum.png
The output i am getting looks like the following:
but according to the debug messages it is setting the icons for each JTree element below the Test
Jtree properly . Do i need to reload the model of the JTree after setting the icon, or is the approach of setting the icon this way wrong in general?