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.

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...
or even more directly via a simple
for-loop...Runnable example