JTree node labels change often but width is cached

I am currently using my own TreeCellRenderer to handle shortcuts and icon changes for my JTree. The problem arises when I need to change the text on several nodes. TreeCellRenderer works great in displaying new text. However, the width from the source text is cached and not updated. I tried to crack it by overriding getPreferredSize in TreeCellRenderer, but it does not work properly, as I have html lines. I even tried to parse the tags and get the width, but it is still not perfect. I used SwingUtilities and FontMetrics.

It seems that using the tree support DefaultTreeModel.nodeChanged (TreeNode) is the correct way to change the node. However, I will need an additional data structure to handle access to nodes that need to be changed. Personally, I would only like to change the nodes that need to be changed. But this will require a different data structure to find all the nodes that need to be updated. What is the best way to cache JLabel database size in this situation?

+7
java swing jtree
source share
1 answer

I found that using what you suggested to call nodeChanged worked better for me.

+2
source share

All Articles