0

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 S.O. pages but none have addressed the problem.

5
  • would wWeb.getEngine().load( urlContent.toExternalForm() + "?server=" + server) work? or is that what you already tried? Commented Jul 29, 2013 at 21:48
  • I just tried what you suggested and no luck. The application will launch, but it will be blank. I think it is still trying to find a file with that exact name. Commented Jul 29, 2013 at 21:54
  • If you try to point your browser (not the JavaFX app) at the full url (i.e. the value of urlContent.toExternalForm() + "?server=" + server), what do you see? Can you post what the value of that full url is? Commented Jul 29, 2013 at 22:27
  • I get a file not found message in firefox. Commented Jul 29, 2013 at 22:48
  • @Sam: Would some kind of event handler be useful here? Something like a Window event with 'onShownProperty()' Commented Jul 29, 2013 at 23:00

1 Answer 1

1

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";
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.