[英]updating JTree in java GUI
I used a JTree in my GUI and added it to a JFrame. 我在GUI中使用了JTree并将其添加到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 问候
In addition to the insertNodeInto suggestion you can also use: 除了insertNodeInto建议,您还可以使用:
DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot();
root.add(new DefaultMutableTreeNode("another_child"));
model.reload(root);
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. 其中一个听众将是
JTree
,一旦收到活动,它将重新粉刷。
For example, DefaultTreeModel
contains the methods: 例如,
DefaultTreeModel
包含以下方法:
nodeChanged
nodesChanged
nodeStructureChanged
nodesWereInserted
nodesWereRemoved
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 . 此外,与所有Swing编程一样,您需要确保在Event Dispatch Thread上更新模型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.