Customization
I have a bunch with intLevels and e levels (both int s) that are stored in the Object s 2D array, heapArray, which intLevels tall and Math.pow(2, intLevels) wide. For hypothetical purposes, let's say I enter 1, 2, 3, 4, 5, 6, 7, 8, and 9. The heap will look something like this:
9 8 6 7 3 2 5 1 4
and if you were to print it with a series of java.util.Arrays.toString(Object[] a) s, it would look like this:
[9, null, null, null, null, null, null, null] [8, 6, null, null, null, null, null, null] [7, 3, 2, 5, null, null, null, null] [1, 4, null, null, null, null, null, null]
Does anyone know how to take this information and create a JTree? For those who donβt know, JTree is very similar to a linked list. You have a root node to which you add more nodes, and you can add additional nodes to them. I know that if the only heap I had to deal with was this one, I could make a tree this way:
jTree = new javax.swing.JTree(); treeNode1 = new javax.swing.tree.DefaultMutableTreeNode(9); treeNode2 = new javax.swing.tree.DefaultMutableTreeNode(8); treeNode3 = new javax.swing.tree.DefaultMutableTreeNode(7); treeNode4 = new javax.swing.tree.DefaultMutableTreeNode(1); treeNode3.add(treeNode4); treeNode4 = new javax.swing.tree.DefaultMutableTreeNode(4); treeNode3.add(treeNode4); treeNode2.add(treeNode3); treeNode3 = new javax.swing.tree.DefaultMutableTreeNode(3); treeNode2.add(treeNode3); treeNode1.add(treeNode2); treeNode2 = new javax.swing.tree.DefaultMutableTreeNode(6); treeNode3 = new javax.swing.tree.DefaultMutableTreeNode(2); treeNode2.add(treeNode3); treeNode3 = new javax.swing.tree.DefaultMutableTreeNode(5); treeNode2.add(treeNode3); treeNode1.add(treeNode2); jTree.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
resulting in a tree as follows:
9 β8 ββ7 βββ1 βββ4 ββ3 β6 β2 β5
Edit
I found an implemented response method buildTree(List<Object[]>) :
java.util.List<Object[]> objectArrays = new java.util.ArrayList<Object[]>(); objectArrays.addAll(Arrays.asList(heapArray)); jTree1 = buildTree(objectArrays);
it still does not work; the tree remains empty.
Question
Does anyone know a relatively simple but flexible way to make this 2D array in JTree with this information? If they are executed correctly, entering 1, 2, 3, 4, 5, 6, 7, 8, and 9 into this tree / heap / array should end with the same result as the specific method shown above.