java-在WebView中检测超链接悬停并打印链接

我想从WebView添加悬停的超链接并将其显示在角落.

我该如何实现?它应该像Chromes功能:

实施例 – 铬 – 截图

enter image description here

具有许多链接的示例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来显示超链接的内容,并且这两个控件应放在某种窗格中.
要在鼠标悬停在超链接上时显示内容,需要获取所有属于超链接类型的节点,然后向其添加适当的侦听器.

查看下面的代码,它可以实现您想要实现的功能(部分基于‘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);
  }
}

转载注明原文:java-在WebView中检测超链接悬停并打印链接 - 代码日志