简体   繁体   中英

Java Jtree populated by SQL query

I would like to create a JTree from a two dimensional object array that I will import from a SQL query. This is what the SQL table looks like:

Here is sample of the object:

    Object[][] table = {
        {1, 0, "Root"}, //i=0
        {2, 1, "Node2"}, //i=1
        {3, 1, "Node3"}, //i=2
        {4, 1, "Node4"}, //i=3
        {5, 4, "Node5"}, //i=4
        {6, 4, "Node6"}, //i=5
        {7, 4, "Node7"}, //i=6
        {8, 1, "Node8"}, //i=7
        {9, 1, "Node9"}, //i=8
        {10, 9, "Node10"},}; //i=9

Here is the logic i use to sort the array:

    for (int i = 0; i < table.length; i++) {
        for (int j = i; j < table.length; j++) {
            if (table[i][0] == table[j][1]) {
                System.out.println(table[i][2].toString() + " is parent of " + table[j][2].toString());
            }
        }
    } 

This is what the above displays in the console:

 Root is parent of Node2
 Root is parent of Node3
 Root is parent of Node4
 Root is parent of Node8
 Root is parent of Node9
 Node4 is parent of Node5
 Node4 is parent of Node6
 Node4 is parent of Node7
 Node9 is parent of Node10

I'm struggling with creating the TreeModel, HashTable, Object, etc. that I can use to create the JTree.

I've been stuck on this problem for over a week and I could really use another person's experience right now.

Organize your data into these columns:

CREATE TABLE tree_node
(NodeID int,
ParentID int,
Value varchar(250)
...) //as many values per node as you want

This will allow you to traverse the recordset and populate your JTree.

Take a look at both depth first and breadth first tree traversal.

I'm new to Java so I had to do some research on this problem. The research was really time consuming. I found that the following link has the best solutions for creating JTree from SQL data.

https://forums.oracle.com/forums/thread.jspa?threadID=2225475

Here is the code I used to solve the problem

class myTree extends JPanel {

private DefaultMutableTreeNode rootNode;
private DefaultTreeModel treeModel;

public myTree() {

    super(new GridLayout(1, 1)); //Set the layout for the panel that the jtree.

    //The following object is a sample from a database and was used to validate 
    //the solution below. Make sure the table is properly sorted before you create the 
    //the object from the query.
    Object[][] table = {
        {1, null, "Root"}, //i=0
        {2, 1, "Node2"}, //i=1
        {3, 2, "Node3"}, //i=2
        {4, 3, "Node4"}, //i=3
        {5, 4, "Node5"}, //i=4
        {6, 5, "Node6"}, //i=5
        {7, 6, "Node7"}, //i=6
        {8, 1, "Node8"}, //i=7
        {9, 1, "Node9"}, //i=8
        {10, 9, "Node10"},};    //i=9

    //Create as many nodes as there are rows of data.
    DefaultMutableTreeNode[] node = new DefaultMutableTreeNode[table.length];
    for (int i = 0; i < table.length; i++) {
        node[i] = new DefaultMutableTreeNode(table[i][2].toString());
    }

    rootNode = node[0];   //Set the root node

    //Cycle through the table above and assign nodes to nodes
    for (int i = 0; i < table.length; i++) {
        for (int j = 1; j < table.length; j++) {
            if (table[i][0] == table[j][1]) {
                System.out.println(table[i][2].toString() + " is parent of " + table[j][2].toString());
                node[i].add(node[j]);
            }
        }
    }
    //Creating the tree model. setting the root node.
    treeModel = new DefaultTreeModel(rootNode);
    //Setting the tree model to the JTree
    JTree tree = new JTree(treeModel);
    //adding the tree to the JPanel
    add(tree);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM