-1

I try to represent mongodb collection in javafx treeview and here is my code. This treeView has to have a name of an ingredient as a parent and fields(variables) as children and their children suppose to be values of those variables.

@FXML
protected TreeView  treeView;

public void editIngredient() {

    TreeItem root, name, measurement, calories;
    root = new TreeItem();

    MongoDatabase db = MongoConnection.getMongoDatabase();

    MongoCollection<Document> ingredients = db.getCollection("ingredients");
    MongoCursor<Document> cursor = ingredients.find().iterator();


    while (cursor.hasNext()) {
        // retrieved a doc
        Document ingredient = cursor.next();
        String ingredientName = ingredient.getString("name");
        name = makeBranch(ingredientName, root);

        // measurement
        measurement = makeBranch("measurement", name);
        makeBranch(ingredient.getString("measurement"), measurement);

        //calories
        calories = makeBranch("calories", name);
        makeBranch(ingredient.getDouble("calories"), calories);
    }

    treeView = new TreeView(root);
    treeView.setShowRoot(false);


}

// creating branches
private TreeItem makeBranch(Object title, TreeItem parent){
    TreeItem item = new TreeItem(title);
    parent.getChildren().add(item);
    return item;
}

here is fxml file:

<center>
    <Pane fx:id="mainPane" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: #3c3f41;" BorderPane.alignment="CENTER" >
        <children>
            <TreeView fx:id="treeView" layoutX="14.0" layoutY="14.0" prefHeight="348.0" prefWidth="200.0"/>
            <TextArea layoutX="239.0" layoutY="14.0" prefHeight="205.0" prefWidth="342.0"/>
        </children>
    </Pane>
</center>

But the treeView doesn't show anything..
What's wrong with is? Thank you in advance!

p.s. pls tell if any additional info needed

Kyle Luke
  • 348
  • 5
  • 16

1 Answers1

1

I haven't read the code in detail, but

parent.getChildren().add(parent);

tries to make a tree item (parent) a child of itself. Any attempt to traverse the tree is then going to result in infinite recursion.

Presumably you mean

parent.getChildren().add(item);
James_D
  • 201,275
  • 16
  • 291
  • 322
  • I've updated the question, thank you – Kyle Luke Feb 03 '16 at 21:46
  • Now it is really a very different question. The code you posted looks fine, so if the tree is not displaying the errors are in code you haven't posted (as far as I can tell). – James_D Feb 03 '16 at 22:03
  • yes, it doesn't no more. I have an fxml file with clear treeview and as you may see treeView variable(that is the one with `fx:id="treeView"` in fxml file). This, I can't say, program :) doesn't throw any exception or an error. The treeView simply doesn't update. – Kyle Luke Feb 03 '16 at 22:09
  • Please don't use SO this way. You completely changed your question by editing it to one which was answered by my answer to something entirely different (which is, incidentally, answered elsewhere on this site). The aim of Stack Overflow is to create a repository of questions and answers which are useful to many programmers. Your edits have made this make no sense to anyone else. – James_D Feb 03 '16 at 23:04