在 java GUI 中更新 JTree
我在 GUI 中使用了 JTree 并将其添加到 JFrame 中。当我想更新它并更改程序另一部分中的节点时(当程序运行时,作为执行的操作),我尝试添加新节点或删除节点;但我的界面没有改变。请给我建议一个解决方案。
问候
I used a JTree in my GUI and added it to a JFrame. When I want to update it and change it's nodes in another part of my program (while program is running, as an action performed) I try to add new nodes, or remove nodes to it; But my interface doesn't change. Please suggest me a solution.
regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除了 insertNodeInto 建议之外,您还可以使用:
In addition to the insertNodeInto suggestion you can also use:
您需要确保在更新模型后指示它触发事件以使所有注册的侦听器收到该事件的通知。 JTree 是侦听器之一,收到事件后它将重新绘制。
例如,
DefaultTreeModel
包含方法:nodeChanged
nodesChanged
nodeStructureChanged
节点插入
nodesWereRemoved
另外,与所有 Swing 编程一样,您需要确保在 < strong>事件调度线程。
You need to ensure that after updating your model you instruct it to fire an event to cause any registered listeners to be notified of the event. One of the listeners will be the
JTree
and upon receiving the event it will repaint.For example,
DefaultTreeModel
contains the methods:nodeChanged
nodesChanged
nodeStructureChanged
nodesWereInserted
nodesWereRemoved
Also, as with all Swing programming you need to ensure you are updating your model on the Event Dispatch Thread.
您的意思是 GUI 方面没有显示您的更改吗?您可能应该查看
repaint()
和revalidate()。
这里很好地描述了何时调用哪个。
Do you mean the GUI aspect just isn't showing your change? You should probably look int
repaint()
andrevalidate().
Here's a good description of when to call which one.