Jtree扩展和选择问题
我有两个 JTrees 实例(leftTree 和 rightTree)。用户可以从左树中选择一些节点并将相同的节点添加到右树中。我在添加按钮操作侦听器中有以下代码,用于在添加节点后展开并选择 rightTree 中的节点。
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();
这段代码似乎对某些节点工作正常,但对 leftTree 中的其他一些节点不起作用。问题是,即使执行上述代码后,rightTree仍处于折叠状态,我看不到所选节点。
我尝试过在 JTree 中使用其他方法,例如 setExpandsSelectedPaths(true)、expandPath(new TreePath(node.getParent()))。另外,在执行上述代码后尝试调用 rightTree.repaint() 或 rightTree.validate() 。但问题仍然存在。但是 rightTree.isExpanded(new TreePath(node.getParent())) 返回 true;
我的树大约有7-8层深。请帮我解决这个问题,如果您需要更多信息,请告诉我。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我来说,这种方法是不正确的。 TreePath 实际上是从当前节点到最顶层父节点的节点序列。换句话说,从节点开始的 TreePath 是通过调用 getParet() 创建的,直到到达 tull。
因此,如果您从一棵树中获取一个节点并创建路径,则该路径是原始树中的节点序列,而该路径在第二棵树中是徒劳的,因为路径中的某些节点在目标树模型中不存在。
您需要获取选定的节点并在第二棵树中找到合适的节点。猜测具有相同用户对象的节点。为目标树中找到的节点(如果我们找到它)创建 TreePath 并选择/展开。
For me the approach is incorrect. TreePath is in fact sequence of nodes from the current node to top most parent. In other words TreePath from node is created by calling getParet() till tull is reached.
So if you get a node from one tree and create path the path is sequence of node in the original tree and the path is futile in the second tree because some nodes in the path just don't exists in the target tree model.
You need to get selected node and find an appropriate one in the second tree. Guess a node with the same user object. The for the found node in the target tree (if we found it) create TreePath and select/expand.