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

查看:945
本文介绍了如何在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的答案,并在 如何使用树 ,你可以搜索你的 TreeModel 以递归方式获取 TreePath 到结果节点。一旦你有了所需的路径,就可以很容易地在树中显示它。

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

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天全站免登陆