javaswingjtreetreecellrenderer

Resize of JTree node name when adding an icon dynamically after the tree has been generated


In my code, depending on a condition, a JTree node might or might not have an Icon. My problem is when I want to set the Icon for a node, the size of the node's name is reduced and "..." are appended.

So basically, the node is recaulculating its size based on the icon size and fitting the rest.

How can I prevent this behavior and permit the node to expand its size when an icon is added?

Here is a screenshot of without/with icon:

enter image description here


Solution

  • don't know where you do it, but suspect you do it under the feet of the TreeModel: the LayoutCache does lots of .. well .. caching of sizes, so you have to make sure that it invalidates that cache. The only way to do so is via TreeModelEvents, something like (assuming the model is of type DefaultTreeModel):

    myNodeData.setIcon(...);
    treeModel.nodeChanged(node);