我使用默认的 NetBeans 编辑器创建了一个表单,并放置了一个jTree
在上面。
然后它以某种方式在其中创建了一堆元素,例如“颜色”、“运动”、“食物”。但它不在创建代码中。它来自哪里以及如何编辑它......
即使我这样做jTree1.removeAll();
一切都还在那里...并且我的用于向 jTree 添加新项目的代码不起作用。
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
运行时的内容。
接下来您创建的问题JTree
像这样JTree tree = new JTree()
(根据docs)它有样本节点。创建节点(root、child1、child2)后添加下一行,一切都会正常工作:
DefaultTreeModel model =(DefaultTreeModel) jTree1.getModel();
model.setRoot(root);
而且你不需要打电话jTree1.removeAll();
它被用于其他目的。(docs)
阅读教程JTree
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)