JTree Space and Rendering BugsWhen working with JTree, sometimes the display can become corrupted with extra space, gaps, long lines, and other display problems. Although it often appears that the JTree code itself may be buggy, in the end I have always found that such problems are caused by misuse of the JTree API in one way or another. Until JTree changes to report such abuses, you'll have to watch for the symptoms and fix them by following these guidelines.
The Big Hammer: DefaultTreeModel.reload()DefaultTreeModel has a method reload() which will fix many JTree painting problems, including long lines, gaps, and phantom nodes. Simply call reload() on the model and watch the rendering issues vanish.
However, there are two substantial drawbacks to reload():
One Likely Source: Multiple InsertsIf you call insertNodeInto() multiple times with the same nodes, JTree can get confused and show empty space in the tree. This will be fixed with reload(), but you really should find the offending code and change it to insert the node just once.
Another Possibility: Tree Model CyclesThe first two parameters to insertNodeInto() are easy to accidentally reverse. It is easy to produce a tree with loops, parents in children, etc. This can also cause rendering strangeness in JTree. Carefully check your insertNodeInto() calls to make sure the first param is the new child, and the second param is the existing parent.
Copyright © 2016 Andrew Oliver