JTree与表单生成器 [英] JTree with form builder
问题描述
我使用默认的NetBeans edito创建了一个表单,并在其上放置了jTree
.
然后以某种方式在其中创建一堆元素,例如颜色",运动",食物".但是它不在创建代码中.它来自哪里,如何编辑...
I created a form with default NetBeans edito and put a jTree
on it.
It somehow then creates bunch of elements such as "colors", "sports", "food" in there. But it is not in the creation code. Where is it coming from and how can I edit it...
即使我做了jTree1.removeAll();
一切仍然存在...而且我的代码也没有添加到jTree工作中.
Even if I do jTree1.removeAll();
everything is still there... and non of my code for adding new items to the jTree working.
private void test(java.awt.event.MouseEvent evt) {
//trying to remove all, but it does not remove anything
jTree1.removeAll();
//it does print it in debug meaning that this function is called
System.out.println("qwe");
//create the root node
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
//create the child nodes
DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Child 1");
DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Child 2");
//add the child nodes to the root node
root.add(child1);
root.add(child2);
//now how do I add it to the tree?
//???
}
我需要能够在运行时编辑jTree
内容.
I need to be able to edit jTree
contents at runtime.
推荐答案
接下来的问题是您像JTree tree = new JTree()
这样创建JTree
(根据
Problem in next you create your JTree
like this JTree tree = new JTree()
(according to docs) it has sample nodes. Add next lines after you create your nodes(root,child1,child2) and all will be work:
DefaultTreeModel model =(DefaultTreeModel) jTree1.getModel();
model.setRoot(root);
您也不必调用jTree1.removeAll();
它用于其他目的.(
Also you needn't to call jTree1.removeAll();
it is used for other purposes.(docs)
阅读 JTree
这篇关于JTree与表单生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!