简体   繁体   English

如何在JavaFX ListView中滚动时禁用setOnMouseClicked

[英]How to disable setOnMouseClicked while scrolling in JavaFX ListView

I developed a small Javafx application and deployed in my Android device, I have a ListView which is configured something like this: 我开发了一个小型Javafx应用程序并将其部署在我的Android设备中,我有一个ListView ,其配置如下:

stuboutList.setOnMouseClicked(new EventHandler<MouseEvent>(){
    @Override
    public void handle(MouseEvent event) {
        Dialog.show("You click the ListView!");
    }
});

Here's the problem: Every time I scroll the ListView the Dialog will keep on popping. 这是问题所在:每次我滚动ListView对话框都会不断弹出。

QUESTION: How to disable setOnMouseClicked while SCROLLING? 问题:如何在滚动时禁用setOnMouseClicked

When you scroll a ListView the swipe gesture triggers a Mouse Drag Event. 滚动ListView ,滑动手势会触发鼠标拖动事件。 You can set a flag, when the drag event is detected, and consume the following Mouse Clicked Event. 您可以在检测到拖动事件时设置一个标志,并使用以下鼠标单击事件。

public class ScrollListener {

    private BooleanProperty scrolling;

    public ScrollListener(Node observableNode) {
        scrolling = new ReadOnlyBooleanWrapper(false);

        observableNode.addEventHandler(MouseEvent.DRAG_DETECTED, e -> scrolling.set(true));

        observableNode.addEventFilter(MouseEvent.MOUSE_CLICKED, evt -> {
            if (scrolling.get()) {
                scrolling.set(false);
                evt.consume();
            }
        });

        observableNode.addEventHandler(MouseEvent.MOUSE_EXITED, e -> scrolling.set(false));
    }

    public ReadOnlyBooleanProperty scrollingProperty() {
        return scrolling;
    }

    public boolean isScrolling() {
        return scrolling.get();
    }
}

Another possiblity is to you use Gluon's CharmListView , which takes care of the Mouse Click Event on its own, but (until now) is not as conveniend to use as the standard ListView , eg when you need to access the SelectionModel , as you can see in this question: CharmListView SelectedItemProperty? 另一个可能性是您使用CharmListViewCharmListView ,它自己负责处理Mouse Click Event,但是(直到现在)不如标准ListView那样方便使用,例如,当您需要访问SelectionModel ,如您所见在这个问题: CharmListView SelectedItemProperty?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM