JavaFX WebEngine中的HyperlinkListener [英] HyperlinkListener in JavaFX WebEngine

查看:733
本文介绍了JavaFX WebEngine中的HyperlinkListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去我使用过JEditorPane,现在我正在尝试使用JavaFX WebEngine。如何为显示页面上包含超链接的事件注册监听器(如链接选择或点击链接)?

In the past I used JEditorPane and now I'm trying my best with JavaFX WebEngine. How can I register listeners for events containing hyperlinks on displayed page (like link selection or click on a link)?

在JEditorPane中有addHyperlinkListener方法......

In JEditorPane there was addHyperlinkListener method...

编辑:

我在第一个答案中遵循了建议。这是我的代码:

I followed the advice in the first answer. This is my code:

webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
    public void changed(ObservableValue ov, State oldState, State newState) {
        if (newState == Worker.State.SUCCEEDED) {
                // note next classes are from org.w3c.dom domain
            EventListener listener = new EventListener() {
                public void handleEvent(Event ev) {
                    System.out.println("KLIKNIETO!!!");
                }
            };

            Document doc = webEngine.getDocument();
            Element el = doc.getElementById("a");
            NodeList lista = doc.getElementsByTagName("a");
            System.out.println("Liczba elementow: "+ lista.getLength());
            for (int i=0; i<lista.getLength(); i++)
                ((EventTarget)lista.item(i)).addEventListener("click", listener, false);
        }
    }
});

我现在点击链接后收到活动。但是我需要引用点击的链接(以获取它的内容)。我怎样才能实现这一点?

I now receive events after clicking on the links. However I need to get reference to the clicked link (to get it's content). How can I achieve that?

推荐答案

您可以通过使用w3c dom在Java中添加单击事件处理程序来捕获链接单击事件加载相关文档后的类。

You can catch the link click event by adding a click event handler in Java using the w3c dom classes once the relevant document has loaded.

在JavaFX WebView中检测HTML textarea onkeyup事件

您还可以使用JavaScript捕获事件(例如使用 jQuery ),这可能比w3c dom api更容易使用。

You can also catch the events using JavaScript (for example using jQuery), which might be a little easier to work with than the w3c dom api.

如果您使用JavaScript来捕获事件,并且想要反馈事件的通知或从JavaScript到Java的后续处理,您可以使用 JavaScript< => Java bridge api

If you are using JavaScript to catch events and you want to feedback notification of the events or subsequent processing from JavaScript to Java, you can use the JavaScript <=> Java bridge api.

我已经记录了一个请求,以便将这个功能的示例添加到官方WebView教程中。

I've logged a request to get a sample of this functionality added to the official WebView tutorial.

这篇关于JavaFX WebEngine中的HyperlinkListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆