Java:如何以编程方式选择和扩展JTree中的多个节点?

时间:2022-02-28 12:53:49

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.

我有一个JTree和一个awt.Canvas。当我从Canvas中选择多个对象到objList时,我希望所有选定的项目都在JTree中显示为选中状态。这意味着,例如,如果我选择了2个对象,则应扩展其到root的路径,并且每个所选对象也应选择其对应的TreeNode。我的JTree有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; 
}

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.

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

1 个解决方案

#1


15  

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的TreeSelectionModel(使用getter)。将TreeSelectionModel#setSelectionPaths用于多个路径。现在,由于tree.setSelectionPath(tpath),您只设置了一个节点。呼叫。 TreeSelectionModel还具有添加/删除现有选择的方法,...(基本上您将来可能需要的所有内容)。

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

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

#1


15  

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的TreeSelectionModel(使用getter)。将TreeSelectionModel#setSelectionPaths用于多个路径。现在,由于tree.setSelectionPath(tpath),您只设置了一个节点。呼叫。 TreeSelectionModel还具有添加/删除现有选择的方法,...(基本上您将来可能需要的所有内容)。

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

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