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