JTree:有人能找出这段代码有什么问题吗
我正在 Net beans 中开发一个小型桌面应用程序。我将一个 JTree
拖放到我的 JFrame
上,现在我想动态填充这个 JTree
的节点层次结构。为此,我编写了一个返回 DefaultMutableTreeNode
对象的方法。现在我再次用这个对象创建树,但树仍然显示旧的(默认)节点:
DefaultMutableTreeNode root = createJTreeNodes();
jTree1 = new JTree(root);
jTree1.repaint();
jTree1.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
任何人都可以指导我需要更改什么,以便动态填充树吗?
I am developing a small desktop application in Net beans. I drag and drop a JTree
on my JFrame
and now i want to fill the node hierarchy of this this JTree
dynamically. For this i wrote a method which return me DefaultMutableTreeNode
object. Now i again create tree with this object but the tree still shows old (default) nodes:
DefaultMutableTreeNode root = createJTreeNodes();
jTree1 = new JTree(root);
jTree1.repaint();
jTree1.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
Could any one guide that what i need to change, in order to populate tree dynamically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
任何人都可以指导我需要更改什么才能动态填充树吗?
几个不同的选项:
或
Could any one guide that what i need to change, in order to populate tree dynamically?
A couple of different options:
or
首先,您需要删除 JTree 中默认由 netbeans 添加的所有节点。
现在创建新的根并添加所需的所有元素,然后使用上面的代码设置新的根。
First you need to remove all the nodes in JTree which are added by netbeans by default.
Now create new root and add all the elements you want and use the above code to set the new root.