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.
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);
}
}