2012-04-04 73 views
4

我想從將從SQL查詢中導入的二維對象數組創建JTree。這是SQL表的樣子:由SQL查詢填充的Java Jtree

這裏是對象的樣品:

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 

這裏是我使用對數組進行排序的邏輯:

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()); 
      } 
     } 
    } 

這是上面什麼在控制檯顯示:

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 

我很努力創建TreeModel,HashTable,對象等,我可以用來創建JTree。

我一直在這個問題上停留了一個多星期,我現在可以真正使用他人的經驗。

+3

將代碼['Outline'](http://codereview.stackexchange.com/a/4447/6692)是利益? – trashgod 2012-04-04 16:00:32

回答

2

我是Java新手,所以我不得不對這個問題做一些研究。這項研究非常耗時。我發現以下鏈接具有從SQL數據創建JTree的最佳解決方案。

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

這裏是我用於解決問題

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); 
} 
2

整理數據到這些列:

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

這將允許您遍歷記錄並填充JTree的。

看看depth firstbreadth first樹遍歷。

+0

我對錶格的計劃做了一個快速的模擬,發現它比我計劃的更有效地管理數據。我會相應地修改我的表格和數據。我查看了深度/呼吸第一棵樹遍歷方法。我把這個算法放在了正確的方向。現在我只需要更多關於樹構造函數的信息就會最好。 JTree(Vector),JTree(HashTable),JTree(TreeModel)等。 – ForgottenKahz 2012-04-04 17:07:16

+1

請查看此[鏈接](http://docs.oracle.com/javase/tutorial/uiswing/components/tree.html#create )。我會去使用DefaultMutableTreeNode的構造函數。當您先用depth或first first search遍歷樹時,將節點添加到根DefaultMutableTreeNode。 – eabraham 2012-04-04 17:30:28

+0

我真的很努力地從查詢中得到數組,並將它們轉化爲父節點,然後在這些節點上放置更多節點,等等。我很確定TreeModel是實現數據的方式,但不知道如何填充TreeModel以反映數據庫中的數據。 – ForgottenKahz 2012-04-06 01:02:03