Search code examples
javaeclipseswingjtreewindowbuilder

How do I make a component like this in Eclipse Java WindowBuilder?


I would like to make such a tree-like component with many roots. What component should I use? JTree doesnt allow multiple parents.

enter image description here


Solution

  • You can achieve that by using a normal JTree and set the setRootVisible property to false

     jTree.setRootVisible(false);
    

    Example

       JFrame frame = new JFrame();
    
       ///adding mock data
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
        for (int i = 1; i < 5; i++) {
            DefaultMutableTreeNode parent = new DefaultMutableTreeNode("Parent" + i);
            for (int j = 1; j < 5; j++) {
                parent.add(new DefaultMutableTreeNode("Child" + j));
            }
            root.add(parent);
        }
    
        DefaultTreeModel model = new DefaultTreeModel(root);
        JTree tree = new JTree(model);
    
        tree.setRootVisible(false);//To hide root
    
        frame.add(new JScrollPane(tree));
        frame.setTitle("Tree RootHide Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    

    Example Output

    Tree with root hidden mocks as Multiple root