Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reach css and image files from the html page loaded by javafx.scene.web.WebEngine#loadContent?

I have a String HTML content which is loaded into webEngine by loadContent() method. I have also some css and image files used in this page. Although I put these file into the same package of java class, the loaded page cannot find them. Looked for API docs and web, but could not find any appropiate similar solutions. How I load these files?

like image 900
Uluk Biy Avatar asked Jan 19 '12 09:01

Uluk Biy


1 Answers

I just found out that using the <base> tag in the HTML also does the trick:

<html>
    <head>
        <title>The slash at the end of the href is important!</title>
        <base href="file:///absolute/path/to/your/docroot/" />
    </head>
    <body>
        <img src="image.png"/>
    </body>
</html>

If you load the above code via engine.loadContent(String) then image.png will be loaded from /absolute/path/to/your/docroot/image.png.

This method is easier if you need to load multiple resources since you only have to specify the absolute path at a single place.

This has been tested with WebView of Java 8u25.

like image 128
Huxi Avatar answered Sep 19 '22 23:09

Huxi