Adding an overlay is very simple: just put the webview and any panel in StackPane.
Another story is the synchronization of overlay data and web browsing. For this you need to ask webview for the coordinates of the object via javascript. Here is an example that finds the stackoverflow question area and marks it on overlay:

the open WebOverlay class extends the application {
@Override public void start(Stage stage) { StackPane root = new StackPane(); WebView webView = new WebView(); final WebEngine webEngine = webView.getEngine(); Canvas overlay = new Canvas(600,600); overlay.setOpacity(0.5); final GraphicsContext gc = overlay.getGraphicsContext2D(); gc.setFill(Color.RED); root.getChildren().addAll(webView, overlay); stage.setScene(new Scene(root, 600, 600)); webEngine.getLoadWorker().workDoneProperty().addListener((observable, oldValue, newValue) -> { if (newValue.intValue() == 100) { // find coordinates by javascript call JSObject bounds = (JSObject)webEngine.executeScript("document.getElementsByClassName('question-hyperlink')[0].getBoundingClientRect()"); Number right = (Number)bounds.getMember("right"); Number top = (Number)bounds.getMember("top"); Number bottom = (Number)bounds.getMember("bottom"); Number left = (Number)bounds.getMember("left"); // paint on overlaing canvas gc.rect(left.doubleValue(), top.doubleValue(), right.doubleValue(), bottom.doubleValue()); gc.fill(); } }); webEngine.load("http://stackoverflow.com/questions/10894903/how-to-make-an-overlay-on-top-of-javafx-2-webview"); stage.show(); } public static void main(String[] args) { launch(); } }
Sergey Grinev
source share