when I remove a node from a Jtree, sometimes other nodes's jlabels are not refreshed and the text is cut off therefore displaying three dots "..." because the jlabel has the width of the previous node that was removed and the UI didn't refreshed as requested in the code.
I believe I'm managing nodes properly and refreshing the UI properly, but I guess either I'm missing something in my code or I am completely doing it wrong. I'm hoping someone can help me.
Here is an example of my JTree:
RootNode
ParentNodeA
ChildNodeA1
ChildNodeA2
ParentNodeB
ChildNodeB1_hithere
ChildNodeB2_imlost
ChildNodeB3_whoisthere
ChildNodeB4_dude
Say I want to add a node "ChildNodeA3" to ParentNodeA. I call the function addNodeToTree() by providing the JTree object and a string array with the path I want the new node to go, ie: [RootNode, ParentNodeA].
When I want to remove a node, say "ChildNodeB2_imlost", I call the function removeNodeFromTree(tree, [RootNode, ParentNodeB, ChildNodeB2_imlost]) which I provide the jtree object and a string array with the complete path to the node that is to be removed.
When I remove the node, other nodes jlabels UI is not refreshed in their new position and still hold their original width at their last position and now doesn't wrap around the text of their new position, therefore showing the three dots.
In the code below, I'm validating the path and expanding parent nodes as necessary in order to find the node to be removed. Once the node is removed, I used the tree model to reload the parent node of the node that was removed to reload the UI, but still doesn't work.
public void removeNodeFromTree(JTree tree, String[] path)
{
TreePath treepath = null;
for (String section : path)
{
int row = (treepath == null ? 0 : tree.getRowForPath(treepath));
// Expand the current tree path because it seems that if the current tree
// path is collapsed, getNextMatch() returns null even though the node we are
// looking for exists. Not sure if this is a bug in JTree.getNextPath() or if that is the intent.
tree.fireTreeExpanded(treepath);
treepath = tree.getNextMatch(section, row, Position.Bias.Forward);
// Return if the path provided doesn't exist.
if ( treepath == null )
{
return;
}
}
// Get the target node to be removed.
DefaultMutableTreeNode targetNode (DefaultMutableTreeNode)treepath.getLastPathComponent();
// Get the parent of the target node.
DefaultMutableTreeNode nParent = (DefaultMutableTreeNode)targetNode.getParent();
targetNode.removeFromParent();
// Get the tree model from the JTree object and refresh the parent node.
DefaultTreeModel tModel = (DefaultTreeModel)tree.getModel();
tMode.reload(nParent);
}
public void addNodeToTree(JTree tree, String[] path, Object userObject)
{
TreePath treepath = null;
for (String section : path)
{
int row = (treepath == null ? 0 : tree.getRowForPath(treepath));
// Expand the current tree path because it seems that if the current tree
// path is collapsed, getNextMatch() returns null even though the node we are
// looking for exists. Not sure if this is a bug in JTree.getNextPath() or if that is the intent.
tree.fireTreeExpanded(treepath);
treepath = tree.getNextMatch(section, row, Position.Bias.Forward);
// Return if the path provided doesn't exist.
if ( treepath == null )
{
return;
}
}
// Get the parent of the new node to be added.
DefaultMutableTreeNode nParent = (DefaultMutableTreeNode)targetNode.getParent();
nParent.add(new DefaultMutableTreeNode(userObject));
// Get the tree model from the JTree object and refresh the parent node.
DefaultTreeModel tModel = (DefaultTreeModel)tree.getModel();
tMode.reload(nParent);
}
Any ideas? Thank you in advanced.