2016-12-16 199 views
0

我有一個數據庫(代碼,composant,父)的表,其中每個節點都有一個父(父也是一個組件代碼),我需要從選擇動態填充一個treeView從數據庫動態填充javaFX treeView

編譯沒事的時候
@FXML 
private TreeView tree;//declaration of the treeView 
HashMap<Integer, composant> node = new HashMap<>(); //for child nodes 
HashMap<Integer, composant> pere = new HashMap<>(); //for parent nodes 
composant c; //object from component class 

private void fillTree(String idSys) { 
    String query = "SELECT * FROM composant WHERE id=?"; 
    try { 
     ps = cnx.prepareStatement(query); 
     ps.setString(1, idSys); 
     rs = ps.executeQuery(); 

     while (rs.next()) { 
      int code = rs.getInt("code"); 
      String composant = rs.getString("composant"); 
      int parent = rs.getInt("parent"); 
      String niveau = rs.getString("niveau"); 
      int id = rs.getInt("id"); 

      c = new composant(code, composant, niveau, parent, id); 
      node.put(code, c); 
      pere.put(parent, c); 
     } 
     ps.close(); 
     rs.close(); 
    } catch (Exception e) { 
     System.err.println("Error" + e); 
    } 

    TreeItem<String> system = new TreeItem<>(sys); 
    //brows and fill parents node 
    for (Integer k : pere.keySet()) { 
     composant p = pere.get(k); 
     TreeItem<String> parent = new TreeItem<>(); 
     parent.setValue(p.getComposant()); 

     //brows and fill child hashmap 
     for (Integer i : node.keySet()) { 
      composant c = node.get(i); 
      TreeItem<String> noeud = new TreeItem<>(); 
      noeud.setValue(c.getComposant()); 

      if (c.getParent() == k) { 
       //if the parent = 1 it must attach to the root node 
       if (k == 1) { 
        system.getChildren().add(noeud); 
       } else { 
        parent.getChildren().add(noeud); 
       } 
      } 
     } 
    } 
    tree.setRoot(system); 
} 

出現在窗口 上這是我有 enter image description here

+0

我不確定構建樹結構的算法是否正確,但如果在GUI中出現* nothing *,則表明您沒有發佈任何錯誤。 (至少你應該看到你的根節點。)我建議你嘗試創建一個[MCVE];代替訪問數據庫,硬編碼一些'composant'實例並將它們放入地圖中。你應該可以在很少的代碼行中做到這一點。如果這不起作用,那麼你可以在你的問題中發佈完整的例子;如果確實如此,您將能夠縮小問題所在。 –

回答

1

我幾乎可以肯定創建樹結構的邏輯是錯誤的。

如果您只是簡單地創建TreeItem,並通過代碼將其存儲在地圖中,那麼最好。然後遍歷地圖並將其添加到它的父代:

Map<Integer, TreeItem<String>> itemById = new HashMap<>(); 
Map<Integer, Integer> parents = new HashMap<>(); 

while (rs.next()) { 
    int code = rs.getInt("code"); 
    String composant = rs.getString("composant"); 
    int parent = rs.getInt("parent"); 
    String niveau = rs.getString("niveau"); 
    int id = rs.getInt("id"); 

    itemById.put(code, new TreeItem<>(composant)); 
    parents.put(code, parent); 
} 
ps.close(); 
rs.close(); 

TreeItem<String> root = null; 
for (Map.Entry<Integer, TreeItem<String>> entry : itemById.entrySet()) { 
    Integer key = entry.getKey(); 
    Integer parent = parents.get(key); 
    if (parent.equals(key)) { 
     // in case the root item points to itself, this is it 
     root = entry.getValue(); 
    } else { 
     TreeItem<String> parentItem = itemById.get(parent); 
     if (parentItem == null) { 
      // in case the root item has no parent in the resultset, this is it 
      root = entry.getValue(); 
     } else { 
      // add to parent treeitem 
      parentItem.getChildren().add(entry.getValue()); 
     } 
    } 
} 
tree.setRoot(root); 

請注意,上面的代碼假定存在唯一的根存儲在表中。如果該查詢返回一個森林裏每一根應該被添加到TreeView的根項目,只需用替代項目

TreeItem<String> root = new TreeItem<>(); 

初始化root並添加項目root,而不是設置根

// root = entry.getValue(); 
root.getChildren().add(entry.getValue()); 
+0

非常感謝@fabian, – devhicham