存储 jtree 的状态/扩展节点以恢复状态

发布于 2024-09-26 10:29:35 字数 143 浏览 10 评论 0原文

我正在使用 JTree。

我想知道了解 JTree 中扩展哪些节点以保存其状态(即保存所有扩展路径)的最佳方法是什么。这样,如果我调用 model.reload(),Jtree 将不会保持折叠状态,但我将能够向用户恢复其原始状态,即所有展开的节点都将展开。

I am working with 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七堇年 2024-10-03 10:29:35

Santhosh Kumar 是我的 Swing Hacks 首选人员之一。

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

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

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

楠木可依 2024-10-03 10:29:35

您需要存储展开的 TreePaths,并在重新加载 TreeModel 后再次展开它们。所有具有后代的 TreePath 都被视为已扩展。 PS如果您删除了路径,请在重新加载后检查该路径是否仍然可用。

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);
    }
}

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.

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);
    }
}
在你怀里撒娇 2024-10-03 10:29:35

我是 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);
        }   
    }
}

}

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.

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);
        }   
    }
}

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文