JavaFX MongoDB表示TreeView中的集合

时间:2016-02-03 20:38:41

标签: java wpf mongodb javafx treeview

我尝试在javafx treeview中表示mongodb集合,这是我的代码。 这个treeView必须有一个成分的名称作为父项,字段(变量)作为子项,它们的子项假设是这些变量的值。

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

这里是fxml文件:

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

但是treeView没有显示任何内容。
有什么问题?提前谢谢!

P.S。请告诉我们是否还需要其他信息

1 个答案:

答案 0 :(得分:1)

我还没有详细阅读代码,但是

def update(self, instance, validated_data):
    # instance is your Office object
    # You should update the Office fields here
    # instance.field_x = validated_data['field_x']

    # Let's grab the Country object again

    country_iso = validated_data.pop('country')
    country = Country.objects.get(iso=country_iso)

    # Update the Office object
    instance.country = country
    instance.save()

    return instance

尝试使树项(parent.getChildren().add(parent); )成为自己的子项。任何遍历树的尝试都将导致无限递归。

大概是指你的意思

parent