JavaFX TreeView is empty

2019-03-01 21:41发布

I am new to working with JavaFX, and I am trying to create a TreeView to add to a Tab. When I add the TreeView to the Tab, however, it is empty. Below is the code that creates, populates, and adds the TreeView to the Tab.

public ResultView(List<WebPage> results, int resultNum) {
    this.pagesWithResults = results;
    urls = new ArrayList<String>();
    outputs = new ArrayList<>();

    resultsTab = new Tab("Result" + resultNum);

    resultTree = new TreeView<>();
    branches = new ArrayList<>();

    for(WebPage pageWithResults: pagesWithResults) {
        this.urls.add(pageWithResults.getURL());
        this.outputs.add(pageWithResults.getOutput());
    }

    TreeItem<String> root = new TreeItem<>();
    root.setExpanded(true);
    //resultTree.setShowRoot(false);

    for(int i = 0; i < urls.size(); i++) {
        branches.add(makeBranch(urls.get(i), root));
        //System.out.println(urls.get(i));

        for(int j = 0; j < outputs.get(i).size(); j++) {
            makeBranch(outputs.get(i).get(j), branches.get(i));
            //System.out.println(outputs.get(i).get(j));
        }
    }

    resultsTab.setContent(resultTree);
}

public TreeItem<String> makeBranch(String title, TreeItem<String> parent) {
    TreeItem<String> item = new TreeItem<>(title);
    item.setExpanded(true);
    parent.getChildren().add(item);
    return item;
}

The TreeItems are created in the makeBranch method, and added to the provided parent.

Here is what the Tab looks like when added to a Scene:

Blank TreeView

1条回答
劫难
2楼-- · 2019-03-01 22:06

I believe I figured it out. I have neglected to set the root of the TreeView. Adding:

resultTree.setRoot(root);

Adds the root to the tree, and makes the tree visible. Without that, I am returning an empty TreeView.

查看更多
登录 后发表回答