Re: XUL/JavaScript <-> Java
Javier Pedemonte <[email protected]>
| Newsgroups | gmane.comp.mozilla.devel.java |
|---|---|
| Message-ID | <Yf-dnQeXyt2U9SfYnZ2dnUVZ_segnZ2d__1434.34628048471$1169844332$gmane$org@mozilla.org> |
[email protected] wrote: > Now i need to call a Java-method from within my JavaScript and call a > JavaScript-method from Java. > > What is the simplest way to do this? I have read something that it is > possible with JavaXPCOM or LiveConnect. But I don't know what is > easyer, better and how to do it. I Think some example codes would save > a few days of work. Cause for somebody who allready has the code its no > prob to post a sample code that help many people needing it like me The best example of using JavaXPCOM to embed XULRunner is the AJAX Toolkit Framework project (http://www.eclipse.org/atf). This embeds Mozilla in an SWT widget. Take a look at the org.eclipse.atf.mozilla.swt.browser plugin. As for calling between Javascript and Java, the way this is done in JavaXPCOM is through the use of interfaces. If you wish to call JavaScript code, the code must implement a known interface. Then on the Java side, you can call |componentManager.createInstance()| with the JavaScript component's CID value. For example: String NS_IWEBBROWSER_CID = "F1EAC761-87E9-11d3-AF80-00A024FFC08C"; webBrowser = (nsIWebBrowser) componentManager.createInstance( NS_IWEBBROWSER_CID, null, nsIWebBrowser.NS_IWEBBROWSER_IID); In this case, |nsIWebBrowser| is the known interface, and NS_IWEBBROWSER_CID is the component's CID value. Going from JavaScript to Java is the same, with regards to JavaXPCOM. You might also be able to use Liveconnect. Search the web for more info on that. > Another questions I have are: > 1.) > In future I want both, run my App via the embedded GUI in Jave using > the code shown and run my gui in directly as xulrunner standalone app. > So I need a interface between java and javascript that works in both > cases. At the moment, you can only use JavaXPCOM to embed XULRunner in a Java app. I have some code to allow writing extensions in Java (see https://bugzilla.mozilla.org/show_bug.cgi?id=299263), but I haven't had the chance to finish that. In the meantime, to use Java in an extension, you can use Liveconnect: http://developer.mozilla.org/en/docs/Java_in_Firefox_Extensions. > 2.) > When I run my Java-App with the shown code it stops at the line > Code: > appStartup.run(); > > when my gui is displayed until I close the window. > Can I call another Java-method from JavaScript although the app stops > at lthis line? And what if I want to call a JavaScript-method from Java > now? This line of code starts the event loop, so you won't be able to run anything from that thread as long as that is running. The exception is if you have registered any callbacks. For example, in the org.eclipse.atf.mozilla.swt.browser plugin, the class MozillaBrowser implements several Mozilla interfaces (such as nsIWebBrowserChrome). A reference to the MozillaBrowser object is passed to some of the Mozilla methods, such that later on, when that code is called, it calls back into the Java methods of MozillaBrowser. For initiating Java to JavaScript calls, you may need to start a separate thread before calling |appStartup.run()|. > 3.) > Will my app stay non platform specific? Yes, should be able to keep it cross-platform. javier