简体   繁体   English

无法使用 Java netbeans 加载节点 jTree

[英]Cannot load the nodes jTree using Java netbeans

this is the first time i am using jtree using java.这是我第一次使用使用 java 的 jtree。 but i couldn't load it when i tried load it it shown as blank.i dont know how to load it.what i tried so far i attached below.please give me the solution for it thanks.但是当我尝试加载它时无法加载它显示为空白。我不知道如何加载它。到目前为止我尝试了什么我附在下面。请给我解决方案谢谢。 JTree name is jTree1 JTree 名称是 jTree1

DefaultTreeModel root1;
     DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root"); 
        public void Load()
        {
            DefaultMutableTreeNode vegetableNode = new DefaultMutableTreeNode("Vegetables");
            vegetableNode.add(new DefaultMutableTreeNode("Capsicum"));
            vegetableNode.add(new DefaultMutableTreeNode("Carrot"));
            vegetableNode.add(new DefaultMutableTreeNode("Tomato"));
            vegetableNode.add(new DefaultMutableTreeNode("Potato"));
            DefaultMutableTreeNode fruitNode = new DefaultMutableTreeNode("Fruits");
            fruitNode.add(new DefaultMutableTreeNode("Banana"));
            fruitNode.add(new DefaultMutableTreeNode("Mango"));
            fruitNode.add(new DefaultMutableTreeNode("Apple"));
            fruitNode.add(new DefaultMutableTreeNode("Grapes"));
            fruitNode.add(new DefaultMutableTreeNode("Orange")); 
            root.add(vegetableNode);
            root.add(fruitNode); 
            root1 = (DefaultTreeModel)jTree1.getModel();
        }

You need to add the tree to the root, then add the tree to the JTree :您需要将树添加到根,然后将树添加到JTree

root1 = (DefaultTreeModel)jTree1.getModel();

To:至:

root1.setRoot(root);
jTree1.setModel(root1);

See the setModel() documentation .请参阅setModel()文档

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

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