Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

web

javafx

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);
like image 908
user2799603 Avatar asked Nov 22 '13 16:11

user2799603


1 Answers

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

like image 68
jewelsea Avatar answered Oct 02 '22 08:10

jewelsea