JTree node labels change often but width is cached
Asked Answered
E

1

8

Currently, I'm using a custom TreeCellRenderer to handle label and icon changes for my JTree. The problem comes when I need to change the text on a few nodes. The TreeCellRenderer works great in that the new text is displayed. However, the width from the initial text is cached and is not updated. I've attempted to hack it by overriding getPreferredSize in the TreeCellRenderer but that's not working properly since I have html strings. I've even tried parsing out the tags and getting the width, but it's still not perfect. I've used SwingUtilities, and FontMetrics.

It seems that using the tree's backing DefaultTreeModel.nodeChanged(TreeNode) is the proper way to change a node. However, I would need to have an additional data structure to handle accessing the nodes that need to be changed. Personally, I would like to only change the nodes that need changing. But, that would require another data structure to find all nodes that need to be updated kinda thing. What's the best way around the size caching of the backing JLabel in this situation?

Ejective answered 12/2, 2010 at 22:13 Comment(1)
I was able to fix this by JTree.setLargeModel(true) the docs say this disables a lot of the caching. But it also warns not all UIs support it. I just tried it in several LnF's as well as on Linux and Windows. Seems alright to me. Any thoughts?Ejective
R
3

I've found that using the way you suggested of calling nodeChanged has worked best for me.

Reseta answered 13/2, 2010 at 15:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.