import javafx.application.Application; import javafx.beans.InvalidationListener; import javafx.beans.Observable; import javafx.collections.ObservableList; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.RadioButton; import javafx.scene.control.ToggleGroup; import javafx.scene.control.TreeItem; import javafx.scene.control.TreeView; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class TreeViewApp extends Application { public static void main(String[] args) { launch(args); } private String type = "Buttons"; TreeItem treeNode; javafx.scene.control.Toggle toggleSelect; @Override public void start(Stage stage) throws Exception { treeNode = new TreeItem("Controls"); treeNode.getChildren().addAll(getTreeViewItems(type)); final ToggleGroup toggleGroup = new ToggleGroup(); final RadioButton radiobutton = new RadioButton(); final RadioButton radiobutton2 = new RadioButton(); final RadioButton radiobutton3 = new RadioButton(); final HBox hbox2 = new HBox(); radiobutton.setToggleGroup(toggleGroup); radiobutton.setText("Buttons"); radiobutton.setSelected(true); radiobutton2.setToggleGroup(toggleGroup); radiobutton2.setText("Form Elements"); radiobutton3.setToggleGroup(toggleGroup); radiobutton3.setText("Lists"); hbox2.getChildren().clear(); hbox2.getChildren().addAll(radiobutton, radiobutton2, radiobutton3); final TreeView treeView = new TreeView(); treeView.setRoot(treeNode); toggleGroup.selectedToggleProperty().addListener(new InvalidationListener() { public void invalidated(Observable ov) { if (ov == null || toggleGroup.getSelectedToggle() == null) { return; } toggleSelect = toggleGroup.getSelectedToggle(); type = ((RadioButton) toggleSelect).getText(); treeNode.getChildren().clear(); treeNode.getChildren().addAll(getTreeViewItems(type)); } }); treeView.setPrefWidth(230.0F); treeView.setPrefHeight(260.0F); stage.setTitle("TreeView Test"); VBox vbox = new VBox(10.0F); vbox.setLayoutX(5.0F); vbox.setLayoutY(5.0F); vbox.getChildren().clear(); vbox.getChildren().addAll(hbox2, treeView); Scene scene = new Scene(new Group(vbox), 240.0F, 320.0F); stage.setScene(scene); stage.show(); } private ObservableList getTreeViewItems(String type) { if (type.equals("Buttons")) { ObservableList treeitems = javafx.collections.FXCollections.observableArrayList(); TreeItem treeitem2 = new TreeItem("Button"); TreeItem treeitem3 = new TreeItem("ButtonBar"); TreeItem treeitem4 = new TreeItem("LinkBar"); TreeItem treeitem5 = new TreeItem("LinkButton"); TreeItem treeitem6 = new TreeItem("PopUpButton"); TreeItem treeitem7 = new TreeItem("ToggleButtonBar"); treeitems.addAll(treeitem2, treeitem3, treeitem4, treeitem5, treeitem6, treeitem7); return treeitems; } else if (type.equals("Form Elements")) { ObservableList treeitems2 = javafx.collections.FXCollections.observableArrayList(); TreeItem treeitem8 = new TreeItem("CheckBox"); TreeItem treeitem9 = new TreeItem("ComboBox"); TreeItem treeitem10 = new TreeItem("DateChooser"); TreeItem treeitem11 = new TreeItem("NumericStepper"); TreeItem treeitem12 = new TreeItem("RadioButton"); TreeItem treeitem13 = new TreeItem("TextArea"); TreeItem treeitem14 = new TreeItem("TextInput"); treeitems2.addAll(treeitem8, treeitem9, treeitem10, treeitem11, treeitem12, treeitem13, treeitem14); return treeitems2; } else if (type.equals("Lists")) { ObservableList treeitems3 = javafx.collections.FXCollections.observableArrayList(); TreeItem treeitem15 = new TreeItem("List"); TreeItem treeitem16 = new TreeItem("Tree"); treeitems3.addAll(treeitem15, treeitem16); return treeitems3; } else { ObservableList treeitems4 = javafx.collections.FXCollections.observableArrayList(); TreeItem treeitem17 = new TreeItem("ColorPicker"); TreeItem treeitem18 = new TreeItem("DataGrid"); TreeItem treeitem19 = new TreeItem("MenuBar"); TreeItem treeitem20 = new TreeItem("ProgressBar"); TreeItem treeitem21 = new TreeItem("ToolTip"); treeitems4.addAll(treeitem17, treeitem18, treeitem19, treeitem20, treeitem21); return treeitems4; } } }