java JavaFX WebEngine 中的 HyperlinkListener
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17555937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HyperlinkListener in JavaFX WebEngine
提问by Marco
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,现在我正在努力使用 JavaFX WebEngine。如何为显示页面上包含超链接的事件注册侦听器(如链接选择或单击链接)?
In JEditorPane there was addHyperlinkListener method...
在 JEditorPane 中有 addHyperlinkListener 方法...
EDIT:
编辑:
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?
我现在点击链接后会收到事件。但是我需要参考点击的链接(以获取它的内容)。我怎样才能做到这一点?
采纳答案by jewelsea
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.
加载相关文档后,您可以通过使用 w3c dom 类在 Java 中添加单击事件处理程序来捕获链接单击事件。
See Sergey's example in Detecting HTML textarea onkeyup event in JavaFX WebView.
请参阅在 JavaFX WebView中检测 HTML textarea onkeyup 事件中的Sergey 示例。
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(例如使用jQuery)捕获事件,这可能比 w3c dom 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.
如果您使用 JavaScript 来捕获事件,并且希望将事件通知或后续处理从 JavaScript 反馈到 Java,则可以使用JavaScript <=> Java 桥接 api。
I've logged a request to get a sample of this functionality added to the official WebView tutorial.
我已经记录了一个请求,以获取将此功能的示例添加到官方 WebView 教程中。
回答by IanB
Where you have
你有的地方
System.out.println("KLIKNIETO!!!");
replace with
用。。。来代替
String href = ((Element)ev.getTarget()).getAttribute("href");
to get the URL of the link clicked on.
获取点击的链接的 URL。
This page gives a good example of what you are trying to do:
此页面提供了您正在尝试执行的操作的一个很好的示例:
http://blogs.kiyut.com/tonny/2013/07/30/javafx-webview-addhyperlinklistener
http://blogs.kiyut.com/tonny/2013/07/30/javafx-webview-addhyperlinklistener
回答by Marcel
The easier way would be using the LibFX libaries WebViewHyperlinkListener : https://github.com/CodeFX-org/LibFX/blob/master/src/main/java/org/codefx/libfx/control/webview/WebViewHyperlinkListener.java
更简单的方法是使用 LibFX 库 WebViewHyperlinkListener :https: //github.com/CodeFX-org/LibFX/blob/master/src/main/java/org/codefx/libfx/control/webview/WebViewHyperlinkListener.java