java 使用 Scene Builder 创建 JavaFX TreeView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12438853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a JavaFX TreeView using Scene Builder
提问by user1673554
I'm starting working with JavaFX and wish to use the new tree view (as you can use multiple icons to represent your data - which is what I wish to take advantage of).
我开始使用 JavaFX 并希望使用新的树视图(因为您可以使用多个图标来表示您的数据 - 这是我希望利用的)。
I have created a basic form/scene that has a tree view and one button on it. When this button is pressed I wish to populate the treeview.
我创建了一个基本的表单/场景,上面有一个树视图和一个按钮。当按下此按钮时,我希望填充树视图。
Now, all the examples ive looked at are where the form/scene is generated in code and the treeview is bound to that control....how do I have a pre designed form with Scene builder and populate it from external code?
现在,我看过的所有示例都是在代码中生成表单/场景的地方,并且树视图绑定到该控件......我如何使用场景构建器预先设计表单并从外部代码填充它?
回答by Kevin Kabatra
You could use the following code in a controller class. Inside the FXML file you will need to set the FXID to selectionTreeView. Tested in JDK 8u5 and it worked.
您可以在控制器类中使用以下代码。在 FXML 文件中,您需要将 FXID 设置为 selectionTreeView。在 JDK 8u5 中测试,它工作正常。
@FXML
TreeView selectionTreeView;
@FXML
private void handleButtonAction(ActionEvent event) {
createTree();
}
public void createTree(String... rootItems) {
//create root
TreeItem<String> root = new TreeItem<>("Root");
//root.setExpanded(true);
//create child
TreeItem<String> itemChild = new TreeItem<>("Child");
itemChild.setExpanded(false);
//root is the parent of itemChild
root.getChildren().add(itemChild);
selectionTreeView.setRoot(root);
}
回答by Andy Till
Set the class name (including package) on the root node of your control in scene builder. If you click on, then go to the code tab on the right it is the top field.
在场景构建器中控件的根节点上设置类名(包括包)。如果单击,则转到右侧的代码选项卡,它是顶部字段。
Now set an ID on the TreeView in your control.
现在在控件中的 TreeView 上设置一个 ID。
Now in the controller object add a TreeView field, the variable name should be the same as what you set the TreeView ID as in scene builder. Annotate with field with @FXML.
现在在控制器对象中添加一个 TreeView 字段,变量名称应该与您在场景构建器中设置的 TreeView ID 相同。使用@FXML 对字段进行注释。
Now when the FXML is loaded, the controller is created and the TreeView field is set.
现在,当加载 FXML 时,将创建控制器并设置 TreeView 字段。