Revision history [back]

click to hide/show revision 1
initial version

answered 2014-02-12 13:02:19 +0800

cor3000 gravatar image cor3000

ZK Team

I don't think there have been fundamental changes to the JS <-> Applet communication in the last 10 years, and also I am not aware of changes to the ZK client API, so I tested myself with a very simple example (I assume you already have your applet running so I'll not bore you with applet security related issues).

assume an applet like this

import java.applet.Applet;
import netscape.javascript.*;
public class TestApplet extends Applet {
    private JSObject js;
    @Override
    public void init() {
        super.start();
        js = JSObject.getWindow(this);
        js.eval("console.log('applet init')");
    }
    public void echoJS(String script) { //called from JS
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        js.eval(script); //callig back into JS
    }
}

with a public method that echoJS that performs in- and outbound communication...

embed the applet in a zk page

<zk>
    <applet id="myapplet" code="TestApplet.class" 
       mayscript="true" onFingerPrint="System.out.println(event.getData());"/>
</zk>

or in your case create the Applet component in Java

    org.zkoss.zul.Applet applet = new org.zkoss.zul.Applet();
    applet.setId("myapplet");
    applet.setMayscript(true);
    applet.setCode("TestApplet.class");
    applet.setParent(yourParentComponent);
            //register a custom event listener
    applet.addEventListener("onFingerPrint", new EventListener<Event>() {
        @Override
        public void onEvent(Event event) throws Exception {
            System.out.println(event.getData());
        }
    });

when you want to call the echoJS method on your applet just call from java:

String script = "jq('@myapplet')[0].echoJS('alert(\"hello something\")')"
Clients.evalJavaScript(script);

if you want are inside your Applet and you want to invoke the onFingerPrint event listener just call

String script = "zk.Widget.$('$myapplet').fire('onFingerPrint', {data: 'somedata'}, {toServer: true})";
js.eval(script);

please let me know if this was helpful.

Regards,

Robert

I don't think there have been fundamental changes to the JS <-> Applet communication in the last 10 years, and also I am not aware of changes to the ZK client API, so I tested myself with a very simple example (I assume you already have your applet running so I'll not bore you with applet security related issues).

assume an applet like this

import java.applet.Applet;
import netscape.javascript.*;
public class TestApplet extends Applet {
    private JSObject js;
    @Override
    public void init() {
        super.start();
        js = JSObject.getWindow(this);
        js.eval("console.log('applet init')");
    }
    public void echoJS(String script) { //called from JS
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        js.eval(script); //callig back into JS
    }
}

with It has a public method that echoJS echoJS that performs in- and outbound communication...

embed the applet in a zk page

<zk>
    <applet id="myapplet" code="TestApplet.class" 
       mayscript="true" onFingerPrint="System.out.println(event.getData());"/>
</zk>

or in your case create the Applet component in Java

    org.zkoss.zul.Applet applet = new org.zkoss.zul.Applet();
    applet.setId("myapplet");
    applet.setMayscript(true);
    applet.setCode("TestApplet.class");
    applet.setParent(yourParentComponent);
            //register a custom event listener
    applet.addEventListener("onFingerPrint", new EventListener<Event>() {
        @Override
        public void onEvent(Event event) throws Exception {
            System.out.println(event.getData());
        }
    });

when you want to call the echoJS method on your applet just call from java:

String script = "jq('@myapplet')[0].echoJS('alert(\"hello something\")')"
Clients.evalJavaScript(script);

if you want are inside your Applet and you want to invoke the onFingerPrint event listener just call

String script = "zk.Widget.$('$myapplet').fire('onFingerPrint', {data: 'somedata'}, {toServer: true})";
js.eval(script);

please let me know if this was helpful.

Regards,

Robert

click to hide/show revision 3
typo and grammar fixes

I don't think there have been fundamental changes to the JS <-> Applet communication in the last 10 years, and also I am not aware of changes to the ZK client API, so I tested myself with a very simple example (I assume you already have your applet running so I'll not bore you with applet security related issues).

assume an applet like this

import java.applet.Applet;
import netscape.javascript.*;
public class TestApplet extends Applet {
    private JSObject js;
    @Override
    public void init() {
        super.start();
        js = JSObject.getWindow(this);
        js.eval("console.log('applet init')");
    }
    public void echoJS(String script) { //called from JS
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        js.eval(script); //callig //calling back into JS
    }
}

It has a public method echoJS that performs in- and outbound communication...

embed the applet in a zk page

<zk>
    <applet id="myapplet" code="TestApplet.class" 
       mayscript="true" onFingerPrint="System.out.println(event.getData());"/>
</zk>

or in your case create the Applet component in Java

    org.zkoss.zul.Applet applet = new org.zkoss.zul.Applet();
    applet.setId("myapplet");
    applet.setMayscript(true);
    applet.setCode("TestApplet.class");
    applet.setParent(yourParentComponent);
            //register a custom event listener
    applet.addEventListener("onFingerPrint", new EventListener<Event>() {
        @Override
        public void onEvent(Event event) throws Exception {
            System.out.println(event.getData());
        }
    });

when When you want to call the echoJS method on your applet just call from java:java (ZK webapp):

String script = "jq('@myapplet')[0].echoJS('alert(\"hello something\")')"
Clients.evalJavaScript(script);

if you want are To fire the "onFingerPrint" event from inside your Applet and you want to invoke the onFingerPrint event listener code just callcall:

String script = "zk.Widget.$('$myapplet').fire('onFingerPrint', {data: 'somedata'}, {toServer: true})";
js.eval(script);

please let me know if this was helpful.

Regards,

Robert

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More