JavaFX WebView - not able to capture scroll action

Viewed 27

I'm trying to capture all scroll actions (scroll by mouse, scroll by dragging scroll bar) for a WebView in JavaFX. I am doing it in the following way, but it's not working. Any suggestions on why it's not working and how to fix this?

WebEngine webEngine = webView.getEngine();
webEngine.getLoadWorker().stateProperty().addListener((ov, oldState, newState) -> {
    if (newState == Worker.State.SUCCEEDED) {
        ScrollBar webViewVerticalScrollBar = getVerticalScrollbar(webView);
        if (webViewVerticalScrollBar != null) {
            webViewVerticalScrollBar
                    .valueProperty()
                    .addListener((observable, oldValue, newValue) -> {
                        System.out.println("webview: " + newValue);
                    });
            scrollBarListenerAddedForItem2 = true;
        }
    }

});
public static ScrollBar getVerticalScrollbar(Node textArea) {
    ScrollBar result = null;
    for (javafx.scene.Node n : textArea.lookupAll(".scroll-bar")) {
        if (n instanceof ScrollBar) {
            ScrollBar bar = (ScrollBar) n;
            if (bar.getOrientation().equals(Orientation.VERTICAL)) {
                result = bar;
            }
        }
    }
    return result;
}

I debugged and found that for the WebView, when I have a scroll bar, getVerticalScrollbar is returning a non-null value, but still the sysout is not getting called even when I scroll using mouse.

0 Answers
Related