R
R
roman38472014-04-17 20:39:03
Java
roman3847, 2014-04-17 20:39:03

JavaFX. Is it possible, when working with a WebView, to change the actions that occur when a link is clicked?

Is there some method that is responsible for handling the click on the link?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Suntechnique, 2014-07-09
@Suntechnique

You can get elements from the webview through the DOM model and register handlers on them:

WebView webView = new WebView();
        WebEngine webEngine = webView.getEngine();
        webEngine.getLoadWorker().stateProperty().addListener(
                ( ov,  oldState, newState) -> {
            if (newState == Worker.State.SUCCEEDED) {
                
                // all next classes are from org.w3c.dom domain
                org.w3c.dom.events.EventListener listener = ( ev) -> {
                    System.out.println("#" + ev.getType());
                };
                
                org.w3c.dom.Document doc = webEngine.getDocument();
                org.w3c.dom.Element el = doc.getElementById("mylink");
                ((org.w3c.dom.events.EventTarget) el).addEventListener("click", listener, false);
            }
        });
        webEngine.loadContent("<a id='mylink' href='google.com'>hi</a>");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question