2

The following code shows a weird behavior in JDK 9.0.1 (Windows):

Dialog Snippet

Dialog<Void> dialog = new Dialog<>();
WebView webView = new WebView();
dialog.getDialogPane().setContent(webView);
webView.getEngine().load("http://docs.oracle.com/javafx/");
dialog.show();

The WebView actually loads, but it does not render the web content to the GUI.

Proof Snippet

I had the following code (I edited it for simplicity sake)

private void loadSite(String url) throws IOException {
    CookieManager cookieManager = new CookieManager();
    CookieHandler.setDefault(cookieManager);
    URI uri = URI.create(url);
    Map<String, List<String>> headers = new HashMap<>();
    CookieHandler.getDefault().put(uri, headers);
    webEngine.getLoadWorker().stateProperty()
        .addListener((observable, oldValue, newValue) -> {
            System.out.println(newValue);
            if (newValue == Worker.State.SUCCEEDED) {
                System.out.println(cookieManager.getCookieStore().getCookies());
            }
        });
    webEngine.load(url);
}

This snippet is used to print cookies from a fully loaded page. The worker reaches State.SUCCEEDED and the cookies get printed out, but the WebView doesn't render the web page.

Working Snippet

I tried the following snippet and it worked, the WebView actually renders the web page to the GUI:

Stage stage = new Stage();
final WebView webView = new WebView();
webView.getEngine().load("http://docs.oracle.com/javafx/");
stage.setScene(new Scene(webView));
stage.show();

The Dialog Snippet works with JDK 8 but not JDK 9.

Side by side comparison

Stage Snippet left, Dialog Snippet Right Stage Snippet left, Dialog Snippet Right

Actual Question

Am I doing something wrong? why is this happening? I read some of the JDK 9 changes, but I didn't see anything relatable.

Thanks in advance for any colaboration.

Community
  • 1
  • 1
LouizFC
  • 161
  • 1
  • 10
  • 1
    I'm having the same issue - the crazy thing is that if you drag the cursor over the java 9 version and then click+drag that, you will see the text being dragged. So it is as if it is rendering, but with no color... – Michael Landes Dec 12 '17 at 09:32
  • @MichaelLandes interesting. I will take some time later to investigate, but maybe, just maybe, applying/reseting css to the WebView Node or Dialog could hack away the problem. – LouizFC Dec 20 '17 at 16:11
  • 1
    @LouizFC I get the same problem when trying to place a WebView in a JavaFX Alert dialog using `getDialogPane().setContent(webView)`... did you fix this? This happened when I switched from Java SDK 8 to SDK 9. –  Apr 17 '18 at 08:48
  • @Antinous unfortunately I didn't. I will gather some time to investigate but I think it is something that can only be worked around and not exactly fixed. – LouizFC Apr 18 '18 at 12:03
  • @LouizFC I ended up using a TextArea in the end, which is not as good as a WebView visually of course, but works nonetheless. –  Apr 18 '18 at 12:34
  • @Antinous If you can add depedencies to your project, I suggest you to see https://github.com/FXMisc/RichTextFX – LouizFC Apr 19 '18 at 13:45

2 Answers2

1

I have similar issue with rendering content into a JfxPanel and it is rendering blank but HTML content can be printed in my java console. (I'm using JxBrowser but they are very similar)

I solved the issue by making the webEngine rendering Mode from Heavy Weight to Light Weight.

Essentially Swing/JavaFx components are light weight for the most part, if we stack a heavy weight component inside a light weight component it will have rendering problems. Hope this helps!

TSZC
  • 11
  • 3
0

I have got exactly the same issue. When I show the WebView in another context (e.g. in a TabView (within a Tab)) the HTML-file is shown properly. But when I use

dialog.getDialogPane().setContent(webView);

it is not. My code is pretty much the same as the Dialog Snippet above (except the location of the HTML-file) and I cannot figure out the problem.

Not even simple HTML with just one headline is displayed.

Marco
  • 59
  • 8
  • did you fix this in the end? I have the same problem with WebView in a JavaFX Alert dialog. `getDialogPane().setContent(webView)` does not show the WebView... –  Apr 17 '18 at 08:49