Creating a JavaFX TreeView using Scene Builder


Question

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

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?

1
3
6/27/2016 9:46:43 AM

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
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);
}
3
4/21/2014 8:58:14 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon