问题描述
我将JTree与TreeNode一起使用,它扩展了DefaultMutableTreeNode.当我添加新节点时,我无法更新JTree.任何帮助将不胜感激
I use JTree with TreeNode which extending DefaultMutableTreeNode.when I add new node,i cant update JTree.Any help will be appreciated
推荐答案
仅添加到节点是不够的,需要通知Tree.正确的方法是让模型负责添加/删除节点,因为它会在途中正确通知Tree.
Adding to the node only is not enough, the Tree needs to be notified. The correct way to do this is to let the model take care of adding/removing nodes, as it will notify correctly the Tree on the way.
如果使用DefaultTreeModel(如果尚未创建自己的TreeModel,则应该是JTree中使用的那个),则可以使用方法 insertNodeInto(MutableTreeNode newChild,MutableTreeNode父级,int索引),您可以使用.
In case of a DefaultTreeModel (which should be the one used in the JTree if you haven't made your own TreeModel), you have the method insertNodeInto(MutableTreeNode newChild, MutableTreeNode parent, int index), which you can use.
这篇关于如何更新JTree元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!