检测WebView中的超链接悬停并打印链接 [英] Detect Hyperlink hover in WebView and print the link
问题描述
我想从WebView添加悬停的超链接并将其显示在角落中。
I want to add the hovered Hyperlink from a WebView and display it in a corner.
我如何实现?它应该像Chromes功能:
How can i achieve? It should be like Chromes feature:
示例 - Chrome-Screenshot
Example-Chrome-Screenshot
包含许多链接的示例WebView代码:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class core extends Application {
@Override
public void start(final Stage stage) {
Scene scene = new Scene(new Group());
final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(browser);
webEngine.load("http://java2s.com");
scene.setRoot(scrollPane);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
推荐答案
首先事情 - 你的场景
应该包含多个元素,而不仅仅是 ScrollPane
。您需要一个 TextField
来显示超链接的内容,这两个 Control
应放在某种窗格上。
要在鼠标移过超链接时处理显示内容,您需要获取所有超链接类型的节点,然后为它们添加适当的侦听器。
First thing - your Scene
should contain more than one element, not only the ScrollPane
. You need a TextField
to show hyperlinks' content and these two Control
s should be placed on some kind of a Pane.
To handle displaying the content when your mouse goes over the hyperlink, you need to get all the nodes, which are of a hyperlink type, and then add proper listeners to them.
查看下面的代码,它可以实现您想要实现的目标(部分基于'灵感与表达'博客中的'JavaFX WebView addHyperlinkListener'文章:
Look at the code below, it does what you would like to achieve (it's partially based on the 'JavaFX WebView addHyperlinkListener' article from the 'Inspiration and Expression' blog):
public class Main extends Application {
@Override
public void start(final Stage stage) {
Scene scene = new Scene(new Group());
final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(browser);
TextField textField = new TextField();
textField.setVisible(false);
textField.setEditable(false);
webEngine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {
if (newValue == Worker.State.SUCCEEDED) {
EventListener mouseOverEventListener = new EventListener() {
@Override
public void handleEvent(Event ev) {
String href = ((Element) ev.getTarget()).getAttribute("href");
textField.setText(href);
textField.setVisible(true);
textField.setPrefWidth(textField.getText().length() * 6); //
System.out.println(href);
}
};
EventListener mouseOutEventListener = new EventListener() {
@Override
public void handleEvent(Event ev) {
textField.setVisible(false);
}
};
Document document = webEngine.getDocument();
NodeList nodeList = document.getElementsByTagName("a");
for (int i = 0 ; i < nodeList.getLength() ; i++) {
((EventTarget) nodeList.item(i)).addEventListener("mouseover",mouseOverEventListener,false);
((EventTarget) nodeList.item(i)).addEventListener("mouseout",mouseOutEventListener,false);
}
}
});
String content = "http://java2s.com";
webEngine.load(content);
AnchorPane anchorPane = new AnchorPane();
anchorPane.getChildren().add(scrollPane);
anchorPane.getChildren().add(textField);
AnchorPane.setBottomAnchor(textField, 0.0);
AnchorPane.setLeftAnchor(textField, 0.0);
scene.setRoot(anchorPane);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
这篇关于检测WebView中的超链接悬停并打印链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!