存储用于恢复状态的jtree的状态/扩展节点。

时间:2021-02-08 12:36:54

I am working with JTree.

我正在和JTree合作。

I would like to know what is best the way to know which nodes are expanded in a JTree so as to save its state (i.e. save all expanded paths). So that if I call model.reload() the Jtree would not stay collapsed, but I will be able to restore its original state to the user, i.e., all expanded nodes will be expanded.

我想知道什么是最好的方法,知道哪些节点在JTree中被扩展,以便保存它的状态(即保存所有扩展路径)。因此,如果我调用model.reload(), Jtree不会保持崩溃,但我将能够恢复它对用户的原始状态,即。所有扩展的节点都将被扩展。

3 个解决方案

#1


8  

Santhosh Kumar is one of my go-to guys for Swing Hacks.

Santhosh Kumar是我最喜欢的摇摆人之一。

Answer: http://www.javalobby.org/java/forums/t19857.html

答:http://www.javalobby.org/java/forums/t19857.html

#2


1  

You need to store the TreePaths that were expanded and expand them again after reloading the TreeModel. All TreePaths that have a descendant are considered to be expanded. P.S. if you deleted paths, check after reloading if the path is still available.

您需要存储在重新加载TreeModel之后扩展并再次扩展的TreePaths。所有有后代的树都被认为是扩张的。如果您删除了路径,如果路径仍然可用,检查重新加载后的路径。

public void reloadTree(JTree jYourTree) {
    List<TreePath> expanded = new ArrayList<>();
    for (int i = 0; i < jYourTree.getRowCount() - 1; i++) {
        TreePath currPath = getPathForRow(i);
        TreePath nextPath = getPathForRow(i + 1);
        if (currPath.isDescendant(nextPath)) {
            expanded.add(currPath);
        }
    }
    ((DefaultTreeModel)jYourTree.getModel()).reload();
    for (TreePath path : expanded) {
        jYourTree.expandPath(path);
    }
}

#3


0  

I'm new to Java and this drove me nuts as well. But I figured it out...I think. Below works fine in my app, but I think it does have some risk of not working as expected in some unusual circumstances.

我是Java新手,这也让我抓狂。但我发现了……我认为。在我的应用程序中,下面的工作很正常,但是我认为在一些不寻常的情况下,它确实有不正常工作的风险。

import javax.swing.JTree;
import javax.swing.tree.TreePath;

public class TreeState {

private final JTree tree;
private StringBuilder sb;

public TreeState(JTree tree){
    this.tree = tree;
}

public String getExpansionState(){

    sb = new StringBuilder();

    for(int i =0 ; i < tree.getRowCount(); i++){
        TreePath tp = tree.getPathForRow(i);
        if(tree.isExpanded(i)){
            sb.append(tp.toString());
            sb.append(",");
        }
    }

    return sb.toString();

}   

public void setExpansionState(String s){

    for(int i = 0 ; i<tree.getRowCount(); i++){
        TreePath tp = tree.getPathForRow(i);
        if(s.contains(tp.toString() )){
            tree.expandRow(i);
        }   
    }
}

}

#1


8  

Santhosh Kumar is one of my go-to guys for Swing Hacks.

Santhosh Kumar是我最喜欢的摇摆人之一。

Answer: http://www.javalobby.org/java/forums/t19857.html

答:http://www.javalobby.org/java/forums/t19857.html

#2


1  

You need to store the TreePaths that were expanded and expand them again after reloading the TreeModel. All TreePaths that have a descendant are considered to be expanded. P.S. if you deleted paths, check after reloading if the path is still available.

您需要存储在重新加载TreeModel之后扩展并再次扩展的TreePaths。所有有后代的树都被认为是扩张的。如果您删除了路径,如果路径仍然可用,检查重新加载后的路径。

public void reloadTree(JTree jYourTree) {
    List<TreePath> expanded = new ArrayList<>();
    for (int i = 0; i < jYourTree.getRowCount() - 1; i++) {
        TreePath currPath = getPathForRow(i);
        TreePath nextPath = getPathForRow(i + 1);
        if (currPath.isDescendant(nextPath)) {
            expanded.add(currPath);
        }
    }
    ((DefaultTreeModel)jYourTree.getModel()).reload();
    for (TreePath path : expanded) {
        jYourTree.expandPath(path);
    }
}

#3


0  

I'm new to Java and this drove me nuts as well. But I figured it out...I think. Below works fine in my app, but I think it does have some risk of not working as expected in some unusual circumstances.

我是Java新手,这也让我抓狂。但我发现了……我认为。在我的应用程序中,下面的工作很正常,但是我认为在一些不寻常的情况下,它确实有不正常工作的风险。

import javax.swing.JTree;
import javax.swing.tree.TreePath;

public class TreeState {

private final JTree tree;
private StringBuilder sb;

public TreeState(JTree tree){
    this.tree = tree;
}

public String getExpansionState(){

    sb = new StringBuilder();

    for(int i =0 ; i < tree.getRowCount(); i++){
        TreePath tp = tree.getPathForRow(i);
        if(tree.isExpanded(i)){
            sb.append(tp.toString());
            sb.append(",");
        }
    }

    return sb.toString();

}   

public void setExpansionState(String s){

    for(int i = 0 ; i<tree.getRowCount(); i++){
        TreePath tp = tree.getPathForRow(i);
        if(s.contains(tp.toString() )){
            tree.expandRow(i);
        }   
    }
}

}