0

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?

Ethyl Casin
  • 791
  • 2
  • 16
  • 34

1 Answers1

1

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, e.g. when you need to access the SelectionModel, as you can see in this question: CharmListView SelectedItemProperty?

Community
  • 1
  • 1
jns
  • 6,017
  • 2
  • 23
  • 28
  • Hi, thanks for the answer, how can I integrate your code to my code above? ;) – Ethyl Casin Apr 26 '16 at 01:41
  • You could call it in the `initialize()` method of the view controller: `new ScrollListener(listView);`. Also keep in mind that you have to remove the eventHandlers / filter if you need to be concerned about resources. – jns Apr 26 '16 at 02:24