从动态数据库中填充javaFX treeView [英] fill javaFX treeView from database dynamically

查看:795
本文介绍了从动态数据库中填充javaFX treeView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a table on database (code, composant, parent) where each node has a parent (a parent is also a component code), I need to fill a treeView dynamically from a select

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

当编译窗口上没有任何内容时出现
这就是我的意思获得

when compiling nothing appear on the window this is what I've got

推荐答案

我几乎可以肯定逻辑创建树结构是错误的。

I'm almost sure the logic of creating the tree structure is wrong.

最好只创建 TreeItem 并按代码存储它们在地图上。然后迭代地图并将每个孩子添加到它的父母:

It's best if you simply create the TreeItems and store them by code in a map. Then iterate over the map and add every child to it's parent:

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 改为使用商品

Note that the above code assumes there is a unique root stored in the table. in case the query returns a forest where each root should be added to the root item of the TreeView, simply initialize root with a item instead

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

并将商品添加到 root 而不是设置根

and add the items to root instead of setting the root

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

这篇关于从动态数据库中填充javaFX treeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆