0

So this is the code that runs the application:

public class TestJavaFX extends Application {
private Scene scene;

@Override
public void start(Stage stage) throws UnsupportedEncodingException, IOException {
    // create the scene
    stage.setMinWidth(550);
    stage.setMinHeight(400);
    stage.setTitle("Dashboard Loading...");
    WebView browser = new WebView();
    // sets temp title of scene & creates WebView, a JavaFX panel
    // Programmatic capabilities (aka DOM/loading pages) are accessed
    // through the WebEngine (browser.getEngine())

    browser.getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
        public void changed(ObservableValue ov, State oldState, State newState) {
            if (newState == State.SUCCEEDED) {
                JSObject win = (JSObject) browser.getEngine().executeScript("window");
                win.setMember("app", new JavaApp());
                HTMLDocument doc = (HTMLDocument) browser.getEngine().getDocument();
                stage.setTitle(doc.getTitle());
            }
        }
    });
    // adds listener to add app object (from java) to the javascript
    // allowing javascript access to java functions
    // and also changes the window title to the HTML title

    browser.getEngine().load(getClass().getResource("html/index.htm").toString());
    // loads the page, resource is configured to run in a jar setting as
    // well as in the IDE

    StackPane layout = new StackPane();
    layout.getChildren().add(browser);
    // creates a layout for the scene and adds the webview

    scene = new Scene(layout, 900, 650);
    new JavaApp().runBatch(5050505);
    // initiates the scene (AKA the inner part of the window) and adds a
    // layout
    // numbers can be changed; they are the size of the window (W,H)

    stage.setScene(scene);
    stage.show();
    // adds scene to stage (window part of window)
    // packs stage and makes stage visible

}

public static void main(String[] args) {
    launch(args);

}

And here is the code for the JavaApp class:

    public class JavaApp{

    public void runBatch(int num) /*throws IOException*/ {
        System.out.println(num);
       }
    }

The HTML file calls runBatch by a button which has an onclick attribute ="app.runBatch(number)" where number could be 0, 1, 2, etc.

Here's the problem: I have no idea why, but whenever runBatch has an argument (e.g. int num) in its definition, it's as if the function doesn't even exist to the webpage and it doesn't run when I click the HTML button. As soon as I remove the argument, it runs fine. Is this a bug or is it something I can fix?

Andrew
  • 1
  • 2
  • Can you post the whole exception trace. – Uluk Biy Aug 20 '15 at 14:14
  • The problem is that there is no exception. There is absolutely no output. Just a quick note: The runBatch within the start method is a test, it outputs the number, but when I press the button in the HTML it doesn't... – Andrew Aug 20 '15 at 14:18
  • Your question may be a dup of [this](http://stackoverflow.com/questions/26715570/passing-values-to-javafx-from-javascript). – Uluk Biy Aug 20 '15 at 14:27
  • Unfortunately that solution does not work after my first try at it. I'll read over it and make sure I didn't miss any important info, but I do not think that when the JavaScript loads has an effect here. – Andrew Aug 20 '15 at 14:45
  • Actually nevermind, that solution was correct. I still don't understand this entirely though, because to me this seems a bit of a separate issue. So if you wouldn't mind clearing things up for me, that would be greatly appreciated ^.^ – Andrew Aug 20 '15 at 17:43
  • 1
    The problem in that question is the app.javaMethod() was invoked at "dom body load" phase. But the setMember("app", new JavaApp()); was called after the html page has been loaded, ie it was not available at body load. however in your case you are invoking it on mouse click, and actually it should work since you are clicking the element after "app" made available. Isn't? – Uluk Biy Aug 21 '15 at 04:18
  • Those were exactly my thoughts. I still don't understand how adding app before made the difference. Maybe because there was a parameter in the onclick, javascript loads it with the page? I've never seen that type of behavior from javascript before... maybe it's just a JavaFX WebView glitch. This is actually quite interesting. – Andrew Aug 23 '15 at 00:42
  • 1
    Can you post the JavaScript portion? – Cypher Aug 27 '15 at 18:07

0 Answers0