Java FX中TreeView节点选中和双击事件监听

TreeItem treeRoot = new TreeItem("Root");
treeRoot.setExpanded(true);
TreeItem exportMr = new TreeItem("Upload to HDFS");
treeRoot.getChildren().add(exportMr);
TreeView treeView = new TreeView(treeRoot);
treeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
    @Override
    public void changed(ObservableValue observable, Object oldValue, Object newValue) {
        TreeItem currentSelectItem = (TreeItem) newValue;
        if (currentSelectItem != null&& currentSelectItem.getValue()=="Upload to HDFS") {
            System.out.println("selection(" + ((TreeItem) newValue).getValue() + ") change");
            ModuleDialog md = new ModuleDialog(primaryStage);
        }
    }
});
treeView.setOnMouseClicked(new EventHandler()
{
    @Override
    public void handle(MouseEvent mouseEvent)
    {
        if(mouseEvent.getClickCount() == 2)
        {
            TreeItem item = treeView.getSelectionModel().getSelectedItem();
            System.out.println("Selected Text : " + item.getValue());

            if (item.getValue()=="Upload to HDFS") {
                ModuleDialog md = new ModuleDialog(primaryStage);
            }
        }
    }
});

 

你可能感兴趣的:(Java FX中TreeView节点选中和双击事件监听)