简体   繁体   English

如何在JTree中将任何节点作为父节点,以便可以向其添加子节点?

[英]How to make a any node as parent node in JTree so that children can be added to it?

How to make current node in JTree as parent node so as to add children to it. 如何在JTree中将当前节点作为父节点,以便向其添加子节点。 My complete data are fetched from the database. 我的完整数据是从数据库中获取的。

somewhat like below : 有点像下面:

root-- parent1 - children
    |- parent2 - children

I need to dynamically generate the tree 我需要动态生成树

Thanks in advance :) 提前致谢 :)

If your nodes are MutableTreeNode eg DefaultMutableTreeNode use 如果您的节点是MutableTreeNode,例如DefaultMutableTreeNode,请使用

public void insert(MutableTreeNode newChild, int childIndex) 

method to add children to current node 将子级添加到当前节点的方法

The usual way to construct a tree is to construct a hierarchy of DefaultMutableTreeNode instances. 构造树的通常方法是构造DefaultMutableTreeNode实例的层次结构。 This class has an add(MutableTreeNode newChild) method which allows adding a child to an existing node. 此类具有add(MutableTreeNode newChild)方法,该方法允许将子级添加到现有节点。

i think this link is what you are looking for: look under 我认为此链接是您正在寻找的:在下面查看

Dynamically Changing a Tree 动态改变树

for a very instructive example, directly from Sun itself! 一个非常有启发性的例子,直接来自Sun本身!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM