存储 jtree 的状态/扩展节点以恢复状态
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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
您需要存储展开的 TreePaths,并在重新加载 TreeModel 后再次展开它们。所有具有后代的 TreePath 都被视为已扩展。 PS如果您删除了路径,请在重新加载后检查该路径是否仍然可用。
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.
我是 Java 新手,这也让我抓狂。但我想通了……我想。下面在我的应用程序中运行良好,但我认为它确实存在在某些异常情况下无法按预期工作的风险。
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.