从动态数据库中填充javaFX treeView

时间:2016-12-16 13:56:09

标签: javafx treeview

我有一个关于数据库(代码,组合,父)的表,其中每个节点都有一个父节点(父节点也是组件代码),我需要从select

动态填充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

1 个答案:

答案 0 :(得分: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的根项,只需使用项目初始化root

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

并将项目添加到root,而不是设置根

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