I have a webView component on a tab in my JavaFX application which I am trying to load an locally stored HTML page into:
WebView browser = new WebView();
WebEngine webEngine = browser.getEngine();
webEngine.load("/webView/main.html");
My html document is (possibly incorrectly) stored in the following location:
where com.cds.gui contains the class where I am attempting to load the file. If I print out webEngine.getDocument()
I get null
- i.e. the document isn't getting loaded.
Please let me know where I'm going wrong! Thanks.
Long tormented with the paths to the file, and this works for me (Maven project, folder resources):
WebEngine engine = html.getEngine();
File f = new File(getClass().getClassLoader().getResource("html/about.htm").getFile());
engine.load(f.toURI().toString());
You need to read the local file in as a URL so that the WebEngine can find it. For instance, you can find the file as a resouce using
URL url = this.getClass().getResource("/com/cds/gui/webView/main.html");
webEngine.load(url.toString());
Or you can load the actual String path into a File object and use it to get the String URL.
File f = new File("full\\path\\to\\webView\\main.html");
webEngine.load(f.toURI().toString());
Hope this helps!
You could use the file syntax for the URI e.g.
file:///C:/path/to/file.html (Windows)
https://en.wikipedia.org/wiki/File_URI_scheme
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With