How to load both html and javascript into webengine from loadContent()?
Asked Answered
R

3

6

Could someone provide some suggestions on how to load the following onto webviewer from loadContent()?

http://jsbin.com/aqupun/6/edit

I was trying to do something like this, but it doesn't seem to work. Thanks!

    Scanner sc1 = new Scanner(new File("src/web/web.html"));
    String webStr = sc1.useDelimiter("\\Z").next();

    Scanner sc2 = new Scanner(new File("src/web/data.js"));
    String dataStr = sc2.useDelimiter("\\Z").next();

    Scanner sc3 = new Scanner(new File("src/web/cytoscape.min.js"));
    String cytoStr = sc3.useDelimiter("\\Z").next();

    Scanner sc4 = new Scanner(new File("src/web/jquery.min.js"));
    String jqueryStr = sc4.useDelimiter("\\Z").next();

    webEngine.loadContent(cytoStr, "text/javascript");
    webEngine.loadContent(jqueryStr, "text/javascript");
    webEngine.loadContent(dataStr, "text/javascript");
    webEngine.loadContent(webStr, "text/html");
Rossi answered 23/11, 2013 at 15:21 Comment(2)
Can you please elaborate by what doesn't seem to work means ? =)Andrew
It eventually works; but you can't have ANY oddities. There's no log that i can detect and not sure how we go about poking and peeking status values from the engine (yet). Just persist.Debase
G
3

You first need to put these three files to the resources on the same level or on the hard drive.

To load your content directly from memory you can use

webView.getEngine().loadContent("your html")

From JavaDoc:

public void loadContent(String content)

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.

Be aware though that the linked resources should be available by their urls, i.e. on disk or in resources. To reflect dynamic changes in your web app I suggest you to call Java from JS. This can be done by providing Java object into JS app: Communication between JavaFX and JavaScript inside WebView, using JSObject

Here you may find a browser demo and a simplified WebView component: Java GUI to display webpages and return HTML.

Gault answered 24/11, 2013 at 12:27 Comment(0)
M
3

I just found out that using the <base> tag in the HTML also does the trick:

<html>
    <head>
        <title>The slash at the end of the href is important!</title>
        <base href="file:///absolute/path/to/your/docroot/" />
    </head>
    <body>
        <img src="image.png"/>
    </body>
</html>

If you load the above code via engine.loadContent(String) then image.png will be loaded from /absolute/path/to/your/docroot/image.png.

This method is easier if you need to load multiple resources since you only have to specify the absolute path at a single place.

This has been tested with WebView of Java 8u25.

Moisesmoishe answered 21/10, 2014 at 13:56 Comment(5)
This sounds like a silly question (but) ... what is docroot when I'm loading a String -- ** .. load( someText, "text/html" )**, I am not so sure it is a silly question after all. I have noted with the load( URI ) version, a socket is opened and volia, everything works. Has anyone come across a tip from the Web Kit project?Debase
@Debase if you're asking what the base would be when you use loadContent, the answer seems to be null. That's what I get when I do <script>document.write(document.baseURI);</script>Verdaverdant
In order to load relative resources (specified in HTML), when using loadContent, it seems <base>'s href needs to be set in HTML before loading.Verdaverdant
Java can get the absolute path to your docroot for you with something like, URL url = YourClass.class.getResource("yourResource.js"); and then getting that resource's parent directory.Verdaverdant
@BradTurek ... thanks for those insights. It appears that when the base is set by the HTML, all bets are off. does document.baseUR give the right value when set in the HTML (document)? I reckon we can't set baseURI -- Or it will have No Effect -- since base is used to load the HTML (as is everything in <head>.... Oh well ...Debase
A
0

You just need to load your HTML page using the load() method of the WebEngine. The loading of the associated CSS and JavaScript will be done for you by the WebEngine.

Here is how I have loaded AceEditor into a WebView:

enter image description here

and the code to do this was just of two lines:

engine = webView.getEngine();
engine.load("file:///home/littlejavachild/Downloads/AceEditor/ace-builds-master/MyTry.html");  

The loading of JavaScript source and CSS is handled for me by the engine.

The docs for the method is here:

public void load(java.lang.String url)

Loads a Web page into this engine. This method starts asynchronous loading and returns immediately.

Parameters: url - URL of the web page to load

Andrew answered 23/11, 2013 at 17:39 Comment(2)
Hi, thanks for your answer. That will work too, but I have a page that is dynamically updated so I want to keep it in memory rather than saving it onto the hard disk in order to load it.Rossi
just supply the url. Try opening your facebook account from webview. It will work like a real browser... no issuesAndrew

© 2022 - 2024 — McMap. All rights reserved.