JavaFX TreeView为空 [英] JavaFX TreeView is empty
问题描述
我不熟悉使用 JavaFX
,我正在尝试创建一个 TreeView
来添加到标签
。但是,当我将 TreeView
添加到选项卡
时,它是空的。下面是创建,填充和添加 TreeView
到标签
的代码。
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;
}
TreeItems
是在 makeBranch
方法中创建,并添加到提供的父
。
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
:
推荐答案
我相信我弄明白了。我忽略了设置 TreeView
的根。添加:
I believe I figured it out. I have neglected to set the root of the TreeView
. Adding:
resultTree.setRoot(root);
将根添加到树中,并使树可见。没有它,我将返回一个空的 TreeView
。
Adds the root to the tree, and makes the tree visible. Without that, I am returning an empty TreeView
.
这篇关于JavaFX TreeView为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!