JTree Space and Rendering Bugs
When 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():
- It collapses the tree. If the user had drilled down deep into
the tree to look at something they'll have to do it all over again.
- It masks the real problem, the misuse of the JTree API elsewhere
One Likely Source: Multiple Inserts
If 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 Cycles
The 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.
Last modified on 5 Dec 2007 by AO
Copyright © 2024 Andrew Oliver