package testjavafx; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.Tooltip; import javafx.scene.control.TreeCell; import javafx.scene.control.TreeItem; import javafx.scene.control.TreeView; import javafx.scene.input.MouseEvent; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage; import javafx.util.Callback; /** * * @author scott */ public class FxMain extends Application { /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Java FX UI Tester - TreeCell eats double-click"); VBox root = new VBox(); TreeItem treeRoot = makeTree(); TreeView treeView = new TreeView(treeRoot); treeView.setCellFactory(cellFactory); root.getChildren().add(new Label("Try to double-click nodes in the tree")); root.getChildren().add(treeView); primaryStage.setScene(new Scene(root)); primaryStage.show(); } private int moveDir = -1; private TreeItem makeTree() { TreeItem x = new TreeItem("Tree Root"); TreeItem branch = new TreeItem("branch"); x.getChildren().add(branch); x.getChildren().add(new TreeItem("other branch")); for (int i = 1; i < 20; i++) { TreeItem ti = new TreeItem("item " + i); if (i == 10) { ti.setValue("item " + i + " - This should move"); } branch.getChildren().add(ti); } return x; } private final Callback cellFactory = new Callback() { @Override public TreeCell call(TreeView p) { TreeCell tc = new GenericTreeCell(); return tc; } }; class GenericTreeCell extends TreeCell { @Override public void updateItem(Object item, boolean empty) { super.updateItem(item, empty); setText(null); setGraphic(null); removeEventHandler(MouseEvent.MOUSE_CLICKED, mouseClicked); if (!empty) { if (item instanceof Node) { setGraphic((Node) item); } else { setText(getString()); setGraphic(getTreeItem().getGraphic()); } addEventHandler(MouseEvent.MOUSE_CLICKED, mouseClicked); } } private String getString() { return getItem() == null ? "" : getItem().toString(); } } private final EventHandler mouseClicked = new EventHandler() { @Override public void handle(MouseEvent t) { System.err.println("Click count = " + t.getClickCount()); } }; }