import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.control.TreeView; import javafx.scene.Group; import javafx.scene.control.Label; import javafx.scene.control.TreeItem; import javafx.scene.layout.VBox; /** * * @author vakulova */ public class TreeViewSimpleTest extends Application { public static void main(String[] args) { launch(args); } static String dataType = "String"; static String keyOrMouse = "Key"; public static TreeView createTreeView(String dataType) { TreeView treeview = new TreeView(); //TODO: Uncomment the following when TreeItem becomes avaialable in the SDK. TreeItem treeitem = new TreeItem(); treeitem.setValue("Controls"); TreeItem treeitem2 = new TreeItem(); treeitem2.setValue("Buttons"); TreeItem treeitem3 = new TreeItem(); treeitem3.setValue("Button"); TreeItem treeitem4 = new TreeItem(); treeitem4.setValue("ButtonBar"); TreeItem treeitem5 = new TreeItem(); treeitem5.setValue("LinkBar"); TreeItem treeitem6 = new TreeItem(); treeitem6.setValue("LinkButton"); TreeItem treeitem7 = new TreeItem(); treeitem7.setValue("PopUpButton"); TreeItem treeitem8 = new TreeItem(); treeitem8.setValue("ToggleButtonBar"); treeitem2.getChildren().clear(); treeitem2.getChildren().addAll(treeitem3, treeitem4, treeitem5, treeitem6, treeitem7, treeitem8); TreeItem treeitem9 = new TreeItem(); treeitem9.setValue("Form Elements"); TreeItem treeitem10 = new TreeItem(); treeitem10.setValue("CheckBox"); TreeItem treeitem11 = new TreeItem(); treeitem11.setValue("ComboBox"); TreeItem treeitem12 = new TreeItem(); treeitem12.setValue("DateChooser"); TreeItem treeitem13 = new TreeItem(); treeitem13.setValue("NumericStepper"); TreeItem treeitem14 = new TreeItem(); treeitem14.setValue("RadioButton"); TreeItem treeitem15 = new TreeItem(); treeitem15.setValue("TextArea"); TreeItem treeitem16 = new TreeItem(); treeitem16.setValue("TextInput"); treeitem9.getChildren().clear(); treeitem9.getChildren().addAll(treeitem10, treeitem11, treeitem12, treeitem13, treeitem14, treeitem15, treeitem16); TreeItem treeitem17 = new TreeItem(); treeitem17.setValue("Lists"); TreeItem treeitem18 = new TreeItem(); treeitem18.setValue("List"); TreeItem treeitem19 = new TreeItem(); treeitem19.setValue("Tree"); treeitem17.getChildren().clear(); treeitem17.getChildren().addAll(treeitem18, treeitem19); TreeItem treeitem20 = new TreeItem(); treeitem20.setValue("ColorPicker"); TreeItem treeitem21 = new TreeItem(); treeitem21.setValue("DataGrid"); TreeItem treeitem22 = new TreeItem(); treeitem22.setValue("MenuBar"); TreeItem treeitem23 = new TreeItem(); treeitem23.setValue("ProgressBar"); TreeItem treeitem24 = new TreeItem(); treeitem24.setValue("ToolTip"); treeitem.getChildren().clear(); treeitem.getChildren().addAll(treeitem2, treeitem9, treeitem17, treeitem20, treeitem21, treeitem22, treeitem23, treeitem24); treeview.setRoot(treeitem); return treeview; } @Override public void start(Stage stage) throws Exception { final Label selectedItemLabel = new Label(); final Label prevSelectedItemLabel = new Label(); final Label testLabel = new Label(); final TreeView treeView = createTreeView(dataType); treeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() { public void changed(ObservableValue ov, Object t, Object t1) { testLabel.setText("Selected Item:" + treeView.getSelectionModel().selectedItemProperty().getValue() + " FocusedItem:" + treeView.getFocusModel().focusedItemProperty().getValue()); if (t != null) prevSelectedItemLabel.setText("Previous selected: " + ((TreeItem) t).getValue().toString()); else prevSelectedItemLabel.setText("Previous selected: NULL"); if (t1 != null) selectedItemLabel.setText("Selected: " + ((TreeItem) t1).getValue().toString()); else selectedItemLabel.setText("Selected: NULL"); } }); stage.setTitle("TreeView Test"); VBox vbox = new VBox(5.0F); vbox.getChildren().clear(); vbox.getChildren().addAll(selectedItemLabel,prevSelectedItemLabel,testLabel, treeView); Group group = new Group(); Scene scene = new Scene(group, 300, 300); ((javafx.scene.Group) scene.getRoot()).getChildren().clear(); ((javafx.scene.Group) scene.getRoot()).getChildren().addAll(vbox); stage.setScene(scene); treeView.requestFocus(); stage.show(); } }