Skip to content

Instantly share code, notes, and snippets.

@micheljung
Last active December 14, 2016 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micheljung/834df7b44d6ac538c85eff28be0da6ce to your computer and use it in GitHub Desktop.
Save micheljung/834df7b44d6ac538c85eff28be0da6ce to your computer and use it in GitHub Desktop.
Demonstration of a bug in the JavaFX WebView (tested with Java 8 u111).In order to see the bug, resize the window. Tested with jdk-9-ea+148_windows-x64_bin.exe as well. Use -Dprism.showdirty=true to see dirty regions.
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.concurrent.Worker.State;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javafx.util.Duration;
import netscape.javascript.JSObject;
import java.util.concurrent.atomic.AtomicInteger;
public class WebViewBug extends Application {
private WebEngine engine;
private AtomicInteger testNumber;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
testNumber = new AtomicInteger();
WebView webView = new WebView();
engine = webView.getEngine();
engine.loadContent("<!DOCTYPE html>\n" +
"<html>\n" +
"<head>\n" +
" <style type=\"text/css\">\n" +
" .row:nth-child(odd) {\n" +
" background: #ccc;\n" +
" }\n" +
" </style>\n" +
"</head>\n" +
"<body id=\"content\">\n" +
"</body>\n" +
"</html>");
primaryStage.setScene(new Scene(webView, 100, 100));
primaryStage.show();
Timeline timeline = new Timeline(
new KeyFrame(Duration.millis(200), (actionEvent)
-> append(("<div class=\"row\">Test " + String.valueOf(testNumber.incrementAndGet()) + "</div>")))
);
timeline.setCycleCount(-1);
engine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {
if (newValue == State.SUCCEEDED) {
timeline.playFromStart();
}
});
}
private void append(String html) {
((JSObject) engine.executeScript("document.getElementById('content')"))
.call("insertAdjacentHTML", "beforeend", html);
engine.executeScript("window.scrollTo(0, document.documentElement.scrollHeight);");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment