Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why can't I add the root to the JTree?

I tried doing JTree jt = new JTree(root); and running it and it says

"The constructor JTree(DefaultMutableTreeNode) is undefined".

Here’s the screenshot of my code – https://snipboard.io/hwU4b7.jpg

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import javax.swing.JFrame;
import javax.swing.tree.DefaultMutableTreeNode;

public class JTreeInfo {

    JFrame frame;
        
    JTreeInfo() {
        frame = new JFrame();   

        DefaultMutableTreeNode root = new DefaultMutableTreeNode("hmtl");
        DefaultMutableTreeNode n1 = new DefaultMutableTreeNode("head");
        DefaultMutableTreeNode n2 = new DefaultMutableTreeNode("body");
        root.add(n1);
        root.add(n2);
        DefaultMutableTreeNode meta = new DefaultMutableTreeNode("meta");
        DefaultMutableTreeNode title = new DefaultMutableTreeNode("title");
        n1.add(meta);
        n1.add(title);
        DefaultMutableTreeNode ul = new DefaultMutableTreeNode("ul");
        DefaultMutableTreeNode hl = new DefaultMutableTreeNode("hl");
        DefaultMutableTreeNode h2 = new DefaultMutableTreeNode("h2");
        n2.add(ul);
        n2.add(hl);
        n2.add(h2);
        DefaultMutableTreeNode li = new DefaultMutableTreeNode("li");
        DefaultMutableTreeNode li2 = new DefaultMutableTreeNode("li");
        ul.add(li);
        ul.add(li2);
        DefaultMutableTreeNode a = new DefaultMutableTreeNode("a");
        h2.add(a);

        JTree jt = new JTree(root); 
        frame.add(jt);
        frame.setSize(500,500);
        frame.setTitle("JTree");
        frame.isVisible(true);
    }

    public static void main(String[] args) {

        new JTreeInfo();

    }
}

>Solution :

Make sure you’re importing the correct packages. A decent IDE will allow you to do this automatically

You should also embed the JTree in a JScrollPane, otherwise you’re going to run into some interesting visual issues

enter image description here

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                DefaultMutableTreeNode root = new DefaultMutableTreeNode("hmtl");
                DefaultMutableTreeNode n1 = new DefaultMutableTreeNode("head");
                DefaultMutableTreeNode n2 = new DefaultMutableTreeNode("body");
                root.add(n1);
                root.add(n2);
                DefaultMutableTreeNode meta = new DefaultMutableTreeNode("meta");
                DefaultMutableTreeNode title = new DefaultMutableTreeNode("title");
                n1.add(meta);
                n1.add(title);
                DefaultMutableTreeNode ul = new DefaultMutableTreeNode("ul");
                DefaultMutableTreeNode hl = new DefaultMutableTreeNode("hl");
                DefaultMutableTreeNode h2 = new DefaultMutableTreeNode("h2");
                n2.add(ul);
                n2.add(hl);
                n2.add(h2);
                DefaultMutableTreeNode li = new DefaultMutableTreeNode("li");
                DefaultMutableTreeNode li2 = new DefaultMutableTreeNode("li");
                ul.add(li);
                ul.add(li2);
                DefaultMutableTreeNode a = new DefaultMutableTreeNode("a");
                h2.add(a);

                JTree jt = new JTree(root);

                JFrame frame = new JFrame();
                frame.add(new JScrollPane(jt));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading