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