简体   繁体   中英

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:

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.

QUESTION: How to disable setOnMouseClicked while SCROLLING?

When you scroll a ListView the swipe gesture triggers a Mouse Drag Event. 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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