1

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 enter image description here

1
  • I'm not sure if the algorithm for building the tree structure is correct, but if nothing is appearing in the GUI, something is wrong that you haven't posted. (At the very least you should see your root node.) I recommend you try to create a minimal reproducible example; instead of accessing a database, hardcode a few composant instances and put them into the maps. You should be able to do that in a pretty small number of lines of code. If that doesn't work then you can post the full example in your question; if it does, you will be able to narrow down where the issue is. Commented Dec 16, 2016 at 14:10

1 Answer 1

2

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

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

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

and add the items to root instead of setting the root

// root = entry.getValue();
root.getChildren().add(entry.getValue());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.