简体   繁体   中英

JavaFX: pass in DOM to Javascript when load() is called from the webEngine

I have been trying to pass a variable that I created so that you could specify which server to connect to when loading a WebView in JavaFX. The way in which my application is set up is that it basically is emulating a webpage in JavaFX. It creates a Scene, sets it to a Stage and calls show() . Initialize is overridden and a URL is created that represents a file path to the HTML file. load() is then called on this URL. Is there any way I can get the Javascript to know about the variable from JavaFX at the time of the load() ?

For example:

public class MyClass implements Initializable
{
  @FXML
  private WebView wWeb;
  private String server = "xyz:server";

  @Override
  public void initialize(URL url, Resource rb)
  {
    URL urlContent = getClass().getResource("index.html");
    wWeb.getEngine().load( urlContent.toExternalForm() );

  }
}

I have tried adding the server after the .html by adding "?server=" + server , but JavaFX thinks that the entire String is the file name and can't find a file with that exact name. It shouldn't be this difficult to pass a DOM from Java to Javascript at load time. Any help would be great. I have been looking for a solution for a while now and have read many SO pages but none have addressed the problem.

SOLVED:

OK boy and girls... found a way to load a page and then set a variable in Javascript. The key to unlocking this was setting a window.status to equal something after loading in Javascript and then add a listener in JavaFX to handle a change in status. When this status changes, I can now call executeScript() . I had to write a function in my Javascript called setServerFromJava() . I left in my test code to help others understand what is going on and when it happens.

The JavaFX:

public class MyClass implements Initializable
{
  @FXML
  private WebView wWeb;
  private String server = "xyz:server";

  @Override
  public void initialize(URL url, Resource rb)
  {
    URL urlContent = getClass().getResource("index.html");
    wWeb.getEngine().load( urlContent.toExternalForm() );

    //I am looking for a change of status so I can inject the DOM
    wWeb.getEngine().setOnStatusChanged(new EventHandler<WebEvent<String>>()
    {
      public void handle(WebEvent<String> status)
      {
        String s = status.getData();
        if(s != null && s.equals( "done" ))
        {
          // the dom is loaded and ready to go
          System.out.println("DID I GET HERE?");
          // javascript
          wWeb.getEngine().executeScript("setServerFromJava('"+ server + "');");
        }
        System.out.println(status);
      }
    });
  }
}

The Javascript:

window.addEventListener("load", function(e)
{
  //DO STUFF HERE...
  //MORE STUFF...

  window.status = "done";
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM