本文介绍了双击JavaFX 2.2时禁用TreeItem的默认展开/折叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发一个JavaFX 2.2项目,我想在TreeItem上设置鼠标(双)单击事件的自定义处理。使用 treeview.setOnMouseClicked 我没有错误地激活我的代码,但问题是TreeItem,在每次鼠标双击时,它在展开和折叠之间切换。我想这是默认行为,但是我如何禁用它?
I am working on a JavaFX 2.2 project and i want to set custom handling of the mouse (double) click event on a TreeItem. Using treeview.setOnMouseClicked i fire my code without errors but the problem is that the TreeItem, on every mouse double click, it toggles between expanded and collapsed. I suppose that this is the default behavior, but how i disable it??
推荐答案
我遇到了同样的问题并解决了它时间使用 EventDispatcher
。
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);
}
}
然后使用此 TreeMouseEventDispatcher
用于 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));
}
}
};
}
}
这篇关于双击JavaFX 2.2时禁用TreeItem的默认展开/折叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!