Re: Extra Row in JTree
On Mar 16, 10:47 am, Jason Cavett <jason.cav...@gmail.com> wrote:
I'm having an issue when adding a new node to a JTree. Here's the
code in question:
this.setEditable(true);
if (parent == null) {
treeModel.insertNodeInto(child, rootNode, rootNode.getChildCount());}=
else {
treeModel.insertNodeInto(child, parent, parent.getChildCount());
}
this.setEditable(false);
I then call "expandPath(e.getTreePath())" inside the TreeModelListener
that I have added to treeModel (which is my own DefaultTreeModel).
Unfortunately, when I do this expandPath, there appears to be an extra
row in the tree. Something like this:
Root
- Item1
- Item2
[BLANK ROW]
- Item3
From the research I did, it appears to be a problem with the
userObject (based on this discussion:http://www.javakb.com/Uwe/Forum.aspx=
/java-gui/6527/JTree-display-upda...),
but I'm really having a problem fixing it. I definitely need the
expandPath to happen, so I'm not sure what else to do.
Anybody have any suggestions on fixing this?
Thanks!
Alright, I'm replying to myself because I figured out a solution, but
I still don't understand what is happening, and I'd like to know.
My solution was, in the TreeModelListener.treeNodesInserted
(TreeModelEvent), do this:
// Expand the path to the node and scroll to the node.
if (e.getTreePath() != null) {
final TreeModelEvent event = e;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
PropertiesTree.this.expandPath(event.getTreePath());
PropertiesTree.this.scrollPathToVisible(event.getTreePath());
}
});
}
It appears that I have to wait for the insertion to finish or
something, but I really don't understand why this is working. Can
anybody explain?
Thank you.