Cache size
JTree uses renderer to render nodes. renderer is the same renderer for all operating systems, so the distinguishing elements are inside ComponentUIs . JTree uses JLabel by default to draw nodes, so its JLabel wo size allows us to cut text using ...
Let's take a short action: Swing has different LookAndFeels for different Operating Systems, they are separated from components in UI classes, for example BasicLabelUI (and this is the source of your problem). BasicLabelUI caches the label size to prevent recounting if no changes have been made. So, BasicLabelUI did not clear the cache of old size values. BasicLabelUI clear the cache if it finds out about any changes.
The question is why BasicLabelUI did not receive change information? Well, if you change / expand / rename the Tree programmatically, you should say that ComponentUI should delete this cache!
You are in luck, you do not need to write a lot of code, because the genius has already written something for you, the creators of the TreeUI class Rob Davis and Scott Violet wrote startEditingAtPath and stopEditing.
Example
TreeUI ui = tree.getUI(); for (TreePath treePath : selectionPaths) { ui.startEditingAtPath(tree, treePath); } tree.setSelectionPaths(selectionPaths); tree.expandPath(expandPaths.getSelectionPath()); ui.stopEditing(layer);
source share