I had the same issue and solved it in time using EventDispatcher
.
class TreeMouseEventDispatcher implements EventDispatcher {
private final EventDispatcher originalDispatcher;
public TreeMouseEventDispatcher(EventDispatcher originalDispatcher) {
this.originalDispatcher = originalDispatcher;
}
@Override
public Event dispatchEvent(Event event, EventDispatchChain tail) {
if (event instanceof MouseEvent) {
if (((MouseEvent) event).getButton() == MouseButton.PRIMARY
&& ((MouseEvent) event).getClickCount() >= 2) {
if (!event.isConsumed()) {
// Implement your double-click behavior here, even your
// MouseEvent handlers will be ignored, i.e., the event consumed!
}
event.consume();
}
}
return originalDispatcher.dispatchEvent(event, tail);
}
}
and then use this TreeMouseEventDispatcher
for the TreeCell
:
treeView.setCellFactory(new Callback<TreeView<T>, TreeCell<T>>() {
@Override
public TreeCell<T> call(TreeView<T> param) {
return new TreeCell<T>() {
@Override
protected void updateItem(T item, boolean empty) {
if (item != null && !empty) {
EventDispatcher originalDispatcher = getEventDispatcher();
setEventDispatcher(new TreeMouseEventDispatcher(originalDispatcher));
}
}
};
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…