如何重置 JTree?
我目前正在使用 JTree,更准确地说是使用 CheckBoxTree,这是一个由 JIDE 创建的继承类。我需要找到一种方法来重置树,这意味着:
- 清除选择
- 删除节点
我尝试取消设置 Tree 变量、treeModel,并刷新 UI,但它不起作用。
有什么想法吗?
I'm currently working with JTree, more precisely with CheckBoxTree, an inherited class created by JIDE. I need to find a way to reset the tree, meaning :
- Clearing the selection
- Erasing the nodes
I tried unsetting the Tree variable, the treeModel, and refreshing the UI, but it doesn't works.
Any ideas ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
单独取消设置变量是没有帮助的 - 所做的只是丢失对 JTree 对象的引用的副本。
您需要做什么来删除包含对象对 JTree 的引用 - 我假设您拥有某种 GridContainer 或 Layout 对象 - 将其从父对象中删除,然后对该对象调用 updateUI 。
Unsetting the variable alone won't help - all that will do is lose your copy of the reference to the JTree object.
What you need to do remove the reference the containing object holds to the JTree - I assume you have it some kind of GridContainer or Layout object - remove it from the parent ojbect and then call updateUI on that object.
为了擦除节点,您应该获取树模型并清除它或设置一个新的树模型。
要清除选择,请在树上调用
clearSelection()
(请注意,setModel(...)
已经调用clearSelection()
,因此如果您想要同时完成这两个操作,只需设置一个新模型并重新绘制)。For erasing the nodes you should get the tree model and clear it or set a new tree model.
To clear the selection, call
clearSelection()
on the tree (note thatsetModel(...)
already callsclearSelection()
so if you want to do both together, just set a new model and repaint).只需在每次迭代开始时重建树即可。
诸如此类的东西
就可以解决问题。无需调用 updateUI。希望这有帮助。
Just rebuild the tree at the beginning of each iteration.
Something of this sort
would do the trick. No need to call updateUI. Hope this help.