Java:如何以编程方式选择和扩展JTree中的多个节点? [英] Java: How to programmatically select and expand multiple nodes in a JTree?

查看:232
本文介绍了Java:如何以编程方式选择和扩展JTree中的多个节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 JTree awt.Canvas 。当我从 Canvas 中选择多个对象进入 objList 时,我希望所有选定的项目都显示在选择 JTree 。这意味着,例如,如果我选择了2个对象,则应扩展它们到root的路径,并且每个选定的对象都应该选择相应的 TreeNode 。我的JTree有 TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION

I have a JTree and an awt.Canvas. When i select multiple objects from within the Canvas into the objList, I want all the selected items to be shown inside the JTree as selected. That means for example if I have 2 objects selected, both their paths to root should be expanded, and also each selected object should have its corresponding TreeNode selected. My JTree has TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION.

以下是我使用的扩展功能的示例:

Here is a sample of the expand funcion i use :

public void selectTreeNodes() {


    HashMap <String, MyEntity> entities = ...;
    Iterator it = entities.keySet().iterator();
    while (it.hasNext()) {

        String str = it.next().toString();
        MyEntity ent = entities.get(str);

        if (ent.isSelected()) {
            DefaultMutableTreeNode searchNode = searchNode(ent.getName());
            if (searchNode != null) {

                TreeNode[] nodes = ((DefaultTreeModel) tree.getModel()).getPathToRoot(searchNode);
                TreePath tpath = new TreePath(nodes);
                tree.scrollPathToVisible(tpath);
                tree.setSelectionPath(tpath);
            }
        }
    }
}

public DefaultMutableTreeNode searchNode(String nodeStr) 
{ 
    DefaultMutableTreeNode node = null; 

    Enumeration enumeration= root.breadthFirstEnumeration(); 
    while(enumeration.hasMoreElements()) {

        node = (DefaultMutableTreeNode)enumeration.nextElement(); 
        if(nodeStr.equals(node.getUserObject().toString())) {

            return node;                          
        } 
    } 

    //tree node with string node found return null 
    return null; 
}

在我当前的状态下,如果我选择一个对象,它将被选中将显示 JTree 及其 TreePath
但是如果实体选择了多个对象,它将不会显示任何内容,我的 JTree 将保持不变。

In my current state, if I select a single object, it will be selected in the JTree and its TreePath will be shown. But if entities has more than 1 object selected, it will display nothing, my JTree will remain unchanged.

推荐答案

您正在寻找<$ c的 TreeSelectionModel $ c> JTree (使用getter)。使用 TreeSelectionModel #setSelectionPaths 。现在,由于您的 tree.setSelectionPath(tpath); 调用,您只设置了一个节点。 TreeSelectionModel 还有添加/删除现有选择的方法,......(基本上你将来可能需要的所有东西)。

You are looking for the TreeSelectionModel of the JTree (use the getter). Use the TreeSelectionModel#setSelectionPaths for multiple paths. Now you are only setting one node selected due to your tree.setSelectionPath(tpath); call. The TreeSelectionModel also has methods to add/remove to an existing selection ,... (basically everything you might need in the future).

一个有趣的扩展方法是 JTree #setExpandsSelectedPaths 方法,允许配置 JTree 自动扩展选定的路径。如果要手动管理,可以使用 JTree #setExpandedState 方法

An interesting method for the expansion is the JTree#setExpandsSelectedPaths method which allows to configure the JTree to automatically expand selected paths. If you want to manage this manually, you can use the JTree#setExpandedState method

这篇关于Java:如何以编程方式选择和扩展JTree中的多个节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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