简体   繁体   English

将元素添加到节点时,保存JTree的扩展状态

[英]Saving expanded state of JTree when adding elements to nodes

Hello I have question regarding saving expansion state when change DefaultTreeModel. 您好,我对更改DefaultTreeModel时保存扩展状态有疑问。 Currently I have a jTree which looks like this: 目前,我有一个jTree看起来像这样: 在此处输入图片说明

If I select an item from the left JList and select a TestGr from right Jtree and click on Add Test Case button it will add the selected items to the DefaultTreeModel. 如果我从左侧的JList中选择一个项目,然后从右侧的Jtree中选择一个TestGr,然后单击“添加测试用例”按钮,它将选择的项目添加到DefaultTreeModel中。 Doing this will collapse the tree: 这样做会使树倒塌: 在此处输入图片说明

I would like to somehow save the previous expansion state and just update the tree to show the newly added element like this for example: 我想以某种方式保存以前的扩展状态,然后更新树以显示新添加的元素,例如: 在此处输入图片说明

My goal is to save the previously expanded state which ever that may be and when I add elements to TestGr, everything which was expanded, should stay expanded and update itself with the new element. 我的目标是保存以前的扩展状态,当我向TestGr添加元素时,所有扩展的内容都应保持扩展状态并使用新元素进行更新。 Does anyone have any idea how might i achieve this? 有谁知道我将如何实现这一目标? I have tried the solution from store state/expanded nodes of a jtree for restoring state but it's not suitable because when i update my DefaultTreeModel, the row count gets updated too and it expands the wrong rows. 我已经尝试从jtree的存储状态/扩展节点恢复状态的解决方案,但是它不适合,因为当我更新DefaultTreeModel时,行数也会更新,并且扩展了错误的行。 Thank you in advance for a hint. 预先感谢您的提示。

Solution is to use DefaultTreeModel reload method, but for a specific node. 解决方案是使用DefaultTreeModel重载方法,但要针对特定​​节点。 for Example: 例如:

DefaultTreeModel model;
DefaultMutableTreeNode node;
//Initialize or change model
model.reload(node);
enter code here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM