Jtree扩展和选择问题

发布于 2024-10-18 03:16:02 字数 895 浏览 2 评论 0原文

我有两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三月梨花 2024-10-25 03:16:02

对我来说,这种方法是不正确的。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文