简体   繁体   中英

GWT hosted mode and applet

It's possible to communicate between applet and GWT application (and vice-versa) when launching GWT in hosted mode? I think no, because applet can communicate only with JavaScript (through LiveConnect), but GWT's hosted mode don't produce any JS...

Although hosted-mode runs part of the code in the jvm, all jsni/dom/native calls are delegated to the browser, so in your case should not be any difference, and you can call the js to communicate to your applet. Of course, what it is not possible is to debug gwt and applet code in the same debugger session.

To call applet methods from GWT, you need a jsni block like:

 private native Object callFoo(String param) /*-{
    var appplet = document.getElementById('myapplet');
    return applet.foo(param);
 }-*/

And to call GWT code from your applet, you need another jsni method to export your gwt code (note that you have to call it once in your entry-point)

 private native void exportBar() /*-{
    window.bar = function(param) {
      return @com.examle.MyClass::myStaticMethod(*)(param);
    }
 }-*/

Anyway, the best approach I know to communicate with applets is using gwtai because all the boiler-plate code needed (wrappers and jsni) is generated automatically.

Gwtai, works in hosted mode without problems and although they say in their site that don't expect it to be stable , It is stable enough and I have used it for a long in production.

GwtAI provides easy to use cross-browser Java Applets integration to Google Web Toolkit (GWT) projects. GwtAI contains a number of utilities and helper classes, such as automatic creation of a wrapper widget and mechanism to communicate with Java Applets.

You need to download two files (GwtAI-Client.jar and GwtAI-Core.jar ) and include in the classpath of your project, then modify your .ui.xml file to inherit gwtai, and you will be able to start coding.

First you have to define the applet interface in your gwt-code, so as GWT compiler using deferred binding creates the comunication class implementation.

 @ImplementingClass(MyClassImpl.class)
 @Height("60") @Width("350") @Archive("GwtAI-Client.jar, MyApp.jar")
 public interface MyApplet extends Applet {
   public Object foo();
 }

Then you have to implement this interface in your applet code.

 public class MyClassImpl extends JApplet implements MyApplet {
   public Object foo(){
      return "Hello";
   }
 }

Finally use the applet from your gwt app

 MyApplet applet = GWT.create(MyApplet.class);
 Object foo = applet.foo();

Read the GettingStarted guide for more info.

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