I have two JTrees instances (leftTree and rightTree). User can select some nodes from left tree and add the same to right tree. I have the below code in add button action listener to expand and select the node in rightTree after the node has been added.
rightTree.updateUI();
TreePath[] paths = leftTree.getSelectionPaths();
if (null != paths && paths.length > 0)
{
TreePath path = paths[0];
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
rightTree.scrollPathToVisible(new TreePath(node.getPath()));
rightTree.setSelectionPaths(paths);
}
leftTree.clearSelection();
This code seems to work fine for some nodes but it cannot work for some other nodes in leftTree. The problem is even after the above code is executed, the rightTree is in collapsed state and I cannot see the selected node.
I have tried using other methods in JTree like setExpandsSelectedPaths(true), expandPath(new TreePath(node.getParent())). Also, tried calling rightTree.repaint() or rightTree.validate() after the above code is executed. But still the problem exists. But rightTree.isExpanded(new TreePath(node.getParent())) retruns true;
My tree is about 7-8 levels deep. Please help me to solve this and let me know if you need more information.