package treetableviewtest; import com.sun.javafx.runtime.VersionInfo; import javafx.application.Application; import static javafx.application.Application.launch; import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleStringProperty; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.EventHandler; import javafx.scene.Scene; 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.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; @Override public void start(Stage primaryStage) { HBox root = new HBox(10.0); Scene scene = new Scene(root, 400, 250); initeTreeTableView(); root.getChildren().addAll(ttv); primaryStage.setTitle(VersionInfo.getRuntimeVersion()); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } private void initeTreeTableView() { ttv = new TreeTableView(); ttv.getFocusModel().focusedIndexProperty().addListener(new ChangeListener() { @Override public void changed(ObservableValue observable, Number oldValue, Number newValue) { System.out.println("Focused index = " + ttv.getFocusModel().getFocusedIndex()); } }); ttv.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); // ttv.getSelectionModel().setCellSelectionEnabled(true); ttv.setRoot(new TreeItem(new TreeTableViewTest.Data("Root", "42"))); ttv.getRoot().setExpanded(true); for (int i = 0; i < 10; i++) { String id = "id - " + i; idPool.add(id); ttv.getRoot().getChildren().add(new TreeItem(new TreeTableViewTest.Data("item - " + i, id))); } 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.setEditable(true); colName.setCellFactory(TextFieldTreeTableCell.forTreeTableColumn()); colId.setCellFactory(ChoiceBoxTreeTableCell.forTreeTableColumn(idPool)); } private class Data { public SimpleStringProperty name; public SimpleStringProperty id; public Data(String _name, String _id) { name = new SimpleStringProperty(_name); id = new SimpleStringProperty(_id); } } }