updating JTree in java GUI

巧了我就是萌 提交于 2019-12-03 12:16:52

In addition to the insertNodeInto suggestion you can also use:

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.

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.

Bryan

Do you mean the GUI aspect just isn't showing your change? You should probably look int repaint() and revalidate().

Here's a good description of when to call which one.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!