I'm working on a task which needs to invoke a java method from a html content. This is a swing application and I used JavaFX WebView to load the HTML content into the application. But when I tried to invoke the Java methods it didn't work and sometime its giving fatal error and crashes the application.
Java class
class Solution extends JFrame {
private JFXPanel jfxPanel;
static JFrame f;
public static void main(String[] args) {
new Solution().createUI();
}
private void createUI() {
f = new JFrame("panel");
JPanel p = new JPanel();
jfxPanel = new JFXPanel();
createScene();
p.add(jfxPanel);
f.add(p);
f.setSize(300, 300);
f.show();
}
private void createScene() {
PlatformImpl.setImplicitExit(false);
PlatformImpl.runAndWait(new Runnable() {
@Override
public void run() {
BorderPane borderPane = new BorderPane();
WebView webComponent = new WebView();
WebEngine webEngine = webComponent.getEngine();
webEngine.load(TestOnClick.class.getResource("/mypage.html").toString());
borderPane.setCenter(webComponent);
Scene scene = new Scene(borderPane,300,300);
jfxPanel.setScene(scene);
JSObject window = (JSObject) webEngine.executeScript("window");
window.setMember("app", new Solution());
}
});
}
public void onClick() {
System.out.println("Invoked from JS");
}
}
HTML
<button onclick="app.onClick()">Click ME</button>
Please let me know what needs to be changed here
From the documentation, both the class and method used for the callback must be
public
:(My emphasis.)
Your
Solution
class is not public, so this won't work.In addition, when a new document is loaded, the
window
will lose its attributes. Since loading happens asynchronously, you need to ensure the member is set on the window after the document loads. You can do this via a listener on thedocumentProperty()
:There are a number of other problems with your code:
JFrame
s must be constructed on the AWT event dispatch thread (the same rule also applies to modifying components displayed in aJFrame
). You can do this by wrapping the call tocreateUI()
inSwingUtilities.invokeLater(...)
.Solution
a subclass ofJFrame
, as well as creating a newJFrame
increateUI()
. Since you never use the fact thatSolution
subclassesJFrame
, you should remove that.PlatformImpl
is not part of the public API: consequently it would be perfectly OK for the JavaFX team to remove that class in a later release. You should use methods in thePlatform
class.Solution
instance, not some arbitrary instance you create. (If you're in an inner class, useSolution.this
to access the current instance of the surrounding object.)A working version of your code is