0

It might be simple but I am stuck with it from last few hours that I need to create the JTree to nth level as per database records. The below code add the childs till level 1, I don't know how can i add subchilds to the nth level, as database comes with parentID of childs.

private DefaultMutableTreeNode setSandboxJTreeModel() {
    try {
        sandboxJTreeRootNode = new DefaultMutableTreeNode(AppConstants.SANDBOX_TREE_NAME);
        //sandboxJTreeModel = new DefaultTreeModel(sandboxJTreeRootNode);
        ArrayList<tbl_bom_sandbox> sandboxArray = daoSandboxObject.fetchAllSandboxRoutes();
        
        DefaultMutableTreeNode parentNode = new DefaultMutableTreeNode(sandboxArray.get(0).getRouteName());
        sandboxJTreeRootNode.add(parentNode);
        
        for (int item = 1; item < sandboxArray.size(); item++) {
            DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(sandboxArray.get(item).getInFeedItemName());
            parentNode.add(childNode);
        }
        
        
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sandboxJTreeRootNode;
}

Current output of code in picture as well as the expected output.

enter image description here

Alpha
  • 167
  • 1
  • 13
  • You need to add each subsequent child node to the previous child node, rather then the parent node – MadProgrammer Aug 15 '23 at 02:56
  • @MadProgrammer: I know about these subsequent, but I don't know how the this will be implemented in java tree. – Alpha Aug 15 '23 at 03:16
  • I did it in [this example](https://stackoverflow.com/questions/76879780/jtree-default-icons-not-removed-with-root-icon/76898575#76898575) – MadProgrammer Aug 15 '23 at 05:06
  • @MadProgrammer: - thank you, but that's not helping me out, as it is static tree with more generalize solution. – Alpha Aug 15 '23 at 22:47
  • I don't want to be disrepectful, but the `populate` method of the linked example is how you would construct the nodes structure you have displayed in your question - the problem is, how construct your original data structure first, because your source structure seems to flatter – MadProgrammer Aug 15 '23 at 23:16
  • Since we don't actually know what the original data structure looks like, it's otherwise impossible for use to provide you with more then high level/conceptual ideas – MadProgrammer Aug 15 '23 at 23:22
  • I do appreciate your solution, but I manage to do this with some different methodology which I know it's not efficient, but I can't waste more time on this. If I found some advance solution I will upgrade my code. Thank you – Alpha Aug 15 '23 at 23:26
  • @MadProgrammer: - The data structure is exactly same as shown in picture, depth of tree will varies depending upon the no of records. – Alpha Aug 15 '23 at 23:29
  • But, how do you determine the relationship between the parent and child - otherwise, it's exactly as I stated, root node -> child node; child node -> new child node, etc, etc - you could easily use a recursive method call as shown in the linked example – MadProgrammer Aug 16 '23 at 00:10
  • @MadProgrammer:- This is exactly I am looking, I know It would be recursive but I was not sure about some calls. Well, I did some tweaks according to DB return values & it works! Really appreciate your help! AWESOME – Alpha Aug 16 '23 at 01:10

1 Answers1

0

The data structure is exactly same as shown in picture, depth of tree will varies depending upon the no of records.

So, conceptually, you want to append the child node to the previous child node.

This could be done using a simple recursive method, simular to...

protected void populate(DefaultMutableTreeNode parent, List<String> items, int index) {
    if (index >= items.size()) {
        return;
    }
    DefaultMutableTreeNode node = new DefaultMutableTreeNode(items.get(index));
    parent.add(node);
    populate(node, items, ++index);
}

or even more directly via a simple for-loop...

DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(items.get(0));
DefaultMutableTreeNode parentNode = rootNode;
for (int index = 1; index < items.size(); index++) {
    DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(items.get(index));
    parentNode.add(childNode);
    parentNode = childNode;
}

Runnable example

enter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

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

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    JFrame frame = new JFrame();
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() throws IOException {
            setLayout(new BorderLayout());
            JTree tree = new JTree();
            tree.setShowsRootHandles(true);

            List<String> items = new ArrayList<>(32);
            items.add("Bill of materials (BOM) SANDBOX");
            items.add("00550R1020AROI_Straightening");
            items.add("00815R1214ARPP");
            items.add("01400R1030ARDWN");
            items.add("01400R1030ARDWN");

            DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(items.get(0));
            populate(rootNode, items, 1);

            DefaultTreeModel model = new DefaultTreeModel(rootNode);
            tree.setModel(model);

            add(new JScrollPane(tree));
        }

        protected void populate(DefaultMutableTreeNode parent, List<String> items, int index) {
            if (index >= items.size()) {
                return;
            }
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(items.get(index));
            parent.add(node);
            populate(node, items, ++index);
        }
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366