import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TreeTableBug1 extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        TreeTableView<String> table = new TreeTableView<>();

        TreeTableColumn<String, String> column = new TreeTableColumn<>("Column");
        column.setPrefWidth(150);
        column.setCellValueFactory(f -> f.getValue().valueProperty());

        // TextArea for logging
        TextArea logArea = new TextArea();
        logArea.setEditable(false);
        logArea.setPrefHeight(500);

        // Cell factory with mouse click handler
        column.setCellFactory(col -> new TreeTableCell<>() {
            {
                setOnMouseClicked(event -> {
                    if (!isEmpty()) {
                        logArea.appendText(
                            "Clicked on: " + getItem() +
                            " | clickCount=" + event.getClickCount() + "\n"
                        );
                    }
                });
            }

            @Override
            protected void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);
                setText(empty ? null : item);
            }
        });

        table.getColumns().add(column);
        table.setPadding(new Insets(10));

        TreeItem<String> rootItem = new TreeItem<>("root");
        rootItem.setExpanded(true);
        rootItem.getChildren().addAll(
                new TreeItem<>("A"),
                new TreeItem<>("B")
        );
        table.setRoot(rootItem);

        VBox root = new VBox(10, table, logArea);
        root.setPadding(new Insets(10));

        Scene scene = new Scene(root, 750, 700);

        scene.getStylesheets().add(getClass().getResource("/tree-table.css").toExternalForm());

        primaryStage.setScene(scene);
        primaryStage.setTitle("TreeTable Mouse Test");
        primaryStage.show();
    }
}