如何在 jtree 中搜索特定节点并使该节点扩展.? [英] How to search a particular node in jtree and make that node expanded.?

查看:31
本文介绍了如何在 jtree 中搜索特定节点并使该节点扩展.?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有 100 个节点的 jtree.现在我想从该树中搜索特定节点并使该节点扩展..?我该如何解决这个问题.?

I am having a jtree with 100 nodes. now i want to search particular node from that tree and make that node expanded..? How can i solve this problem.?

推荐答案

扩展@mKorbel 的回答,并在 How to Use Trees,你可以递归搜索你的TreeModel并获得一个TreePath结果节点.一旦您有了所需的path,就很容易在树中显示它.

Expanding on @mKorbel's answer and as discussed in How to Use Trees, you can search your TreeModel recursively and obtain a TreePath to the resulting node. Once you have the desired path, it's easy to reveal it in the tree.

tree.setSelectionPath(path);
tree.scrollPathToVisible(path);

附录:这是获得 TreePath"的一种方法.

Addendum: Here's one way to "obtain a TreePath."

private TreePath find(DefaultMutableTreeNode root, String s) {
    @SuppressWarnings("unchecked")
    Enumeration<DefaultMutableTreeNode> e = root.depthFirstEnumeration();
    while (e.hasMoreElements()) {
        DefaultMutableTreeNode node = e.nextElement();
        if (node.toString().equalsIgnoreCase(s)) {
            return new TreePath(node.getPath());
        }
    }
    return null;
}

这篇关于如何在 jtree 中搜索特定节点并使该节点扩展.?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆