javafx webview load local css files

css, html, java, javafx, webview

Solution

Putting the css file in the same directory as the html file, will work if

webView.getEngine().load(location);

is used. However it will not work for `loadContent()`. You need to explicitly define the css file location as:

(pseudo-code)

str = str.replace("href='main.css'", 
                  "href='" + getClass().getResource("main.css") + "'");

Problem

I have a `WebView`, which loads a local html file that I have saved within my project. I use the following to load the file: ``` InputStream is = Browser.class.getResourceAsStream(location); String str = ""; int i; while((i = is.read()) != -1){ str += (char)i; } str = str.replace("{placeholder_1}", value1); str = str.replace("{placeholder_2}", value2); webEngine.loadContent(str); ``` In the HTML I have a link to a css file. The HTML file and the css file are in the same directory, but the css file isn't loading when the page loads in the `WebView`. Here is how I am calling the css file from the HTML: ``` <link rel="stylesheet" href="main.css" /> ``` Why is the file not loading? According to others, this is how they are doing it and it is working. Why is it not working for me, what am I doing wrong? Here is the directory layout:

Original source

Related problems