How to load webpage from a string of html code in JavaFX webviewer?


Does JavaFX web viewer support loading a web page from a string of html code? My code is currently functional under scenario 1 below. However, I will need to break the webFile up into two pieces (top & bot), and then insert a string of html in between. The final result is loaded via webviewer. Please see #2 for my intent (it doesn't work). Can anyone suggest on how I might be able to pull this off? Thanks!


String webFileStr = (new File(webFile)).toURI().toURL().toString();


String webStr = topSlice + data + botSlice;
11/22/2013 4:35:25 PM

Accepted Answer

Use WebEngine.loadContent.

webView.getEngine().loadContent("<html>hello, world</html>", "text/html");

Javadoc description:

Loads the given content directly. This method is useful when you have content composed in memory, or loaded from some system which cannot be reached via a URL (for example, the SVG text may have come from a database). As with load(String), this method is asynchronous. This method also allows you to specify the content type of the string being loaded, and so may optionally support other types besides just HTML.

As Hiux suggests in comments:

using a <base> tag is the trick to load relative resources with loadContent(String) as demonstrated in his related answer to How to load both html and javascript into webengine from loadContent()?.

5/23/2017 11:54:11 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow