6

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!

1.

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

2.

String webStr = topSlice + data + botSlice;
webEngine.load(webStr);
user2799603
  • 873
  • 3
  • 13
  • 19

1 Answers1

11

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()?.

Community
  • 1
  • 1
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • oh that's perfect! The html content will be composed in memory, figured loading it directly would be a lot faster than saving it then loading the page. Will I be able to include css/javascript as well? e.g. "css link or embedded, javascript link or embedded (including jquery library) ...etc " – user2799603 Nov 23 '13 at 14:13
  • 3
    [I just found out](http://stackoverflow.com/questions/20164062/how-to-load-both-html-and-javascript-into-webengine-from-loadcontent/26488496#26488496) that using a `` tag is the trick to load relative resources with `loadContent(String)`. I searched quite long for such a solution so I wanted to share it. :) – Huxi Oct 21 '14 at 14:05