package treetableviewtest; import com.sun.javafx.runtime.VersionInfo; import javafx.application.Application; import javafx.application.Platform; import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleStringProperty; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.SelectionMode; import javafx.scene.control.TreeItem; import javafx.scene.control.TreeTableColumn; import javafx.scene.control.TreeTableColumn.CellDataFeatures; import javafx.scene.control.TreeTableView; import javafx.scene.control.cell.ChoiceBoxTreeTableCell; import javafx.scene.control.cell.TextFieldTreeTableCell; import javafx.scene.input.MouseButton; import javafx.scene.input.MouseEvent; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import javafx.stage.Stage; import javafx.util.Callback; public class TreeTableViewTest extends Application { private SimpleIntegerProperty clickCounter = new SimpleIntegerProperty(0); private ObservableList idPool = FXCollections.observableArrayList(); private TreeTableView ttv; Pane root; @Override public void start(Stage primaryStage) { Button btnInit = new Button("Initialize"); btnInit.setOnAction(new EventHandler() { @Override public void handle(ActionEvent t) { initTreeTableView(); } }); root = new HBox(10.0); root.getChildren().add(btnInit); Scene scene = new Scene(root, 400, 300); primaryStage.setTitle(VersionInfo.getRuntimeVersion()); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } private void initTreeTableView() { ttv = new TreeTableView(); root.getChildren().add(ttv); ttv.setRoot(new TreeItem(new Data("Root", "42"))); // ttv.getRoot().setExpanded(true); final TreeTableColumn colName = new TreeTableColumn("Name"); colName.setCellValueFactory(new Callback, ObservableValue>() { @Override public ObservableValue call(CellDataFeatures p) { return p.getValue().getValue().name; } }); colName.setMinWidth(100d); final TreeTableColumn colId = new TreeTableColumn("Id"); colId.setCellValueFactory(new Callback, ObservableValue>() { @Override public ObservableValue call(CellDataFeatures p) { return p.getValue().getValue().id; } }); colId.setMinWidth(75d); ttv.getColumns().addAll(colName, colId); ttv.setOnMouseClicked(new EventHandler() { @Override public void handle(MouseEvent t) { if (t.getButton() == MouseButton.PRIMARY) { clickCounter.set(clickCounter.get() + 1); } } }); ttv.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); ttv.getSelectionModel().setCellSelectionEnabled(true); ttv.setEditable(true); colName.setCellFactory(TextFieldTreeTableCell.forTreeTableColumn()); colId.setCellFactory(ChoiceBoxTreeTableCell.forTreeTableColumn(idPool)); Platform.runLater(new Runnable() { @Override public void run() { for (int i = 0; i < 10; i++) { String id = "id - " + i; idPool.add(id); TreeItem treeItem = new TreeItem(new Data("item - " + i, id)); ttv.getRoot().getChildren().add(treeItem); for (int j = 0; j < 2; j++) { treeItem.getChildren().add(new TreeItem(new Data("item - " + i + " - " + j, id))); } } } }); } private class Data { public SimpleStringProperty name; public SimpleStringProperty id; public Data(String _name, String _id) { name = new SimpleStringProperty(_name); id = new SimpleStringProperty(_id); } } }