0

ZK 6 and Java Applet integration

asked 2014-02-10 23:30:37 +0800

mrojas gravatar image mrojas
17 2

Hello everybody.

I'm looking for a way to integrate a Java Applet into our ZK application in order to get data from a fingerprint reader for a login window. I've read a 2009 smalltalk but as far as I can tell it's already obsoleted. I'm using a single ZUL file as the application entry point (as index.zul), and everything else is being created programatically via the Java ZK API. Any pointers in the right direction will be greatly appreciated.

Best regards and thanks in advance.

delete flag offensive retag edit

Comments

are you having trouble embedding the applet into the website, or do you need hints how to communicate from the applet via JS/Ajax to the server?

cor3000 ( 2014-02-11 02:03:08 +0800 )edit

Yes, it's about the applet communication. As I've said in my post, I'm using just an index.zul file as my application entry point, and from there all the user interface is generated via Java classes. My index.zul file uses a Java class that extends from the Window ZK class (no Composer, just Window)

mrojas ( 2014-02-11 15:41:38 +0800 )edit

Can you provide which API you use for fingerprint

sitansu ( 2014-02-12 16:28:51 +0800 )edit

Hi @sitansu I'm going to use Suprema BioMini readers and their SDK. Regards.

mrojas ( 2014-02-12 18:02:38 +0800 )edit

1 Answer

Sort by » oldest newest most voted
1

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

cor3000 gravatar image cor3000
6280 2 7

updated 2014-02-13 01:33:29 +0800

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); //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 you want to call the echoJS method on your applet just call from java (ZK webapp):

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

To fire the "onFingerPrint" event from inside your Applet code 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

link publish delete flag offensive edit

Comments

I also tested this in the chrome JS console

jq('@myapplet')[0].echoJS("zk.Widget.$('$myapplet').fire('onFingerPrint', {data: 'somedata'}, {toServer: true})");

cor3000 ( 2014-02-12 13:08:52 +0800 )edit

Thanks a lot Robert, I'll try your suggestions and let you know about it.

mrojas ( 2014-02-12 15:02:16 +0800 )edit

Thank you very much Robert, it works as advertised ;) now I can listen to events from the applet on the ZK window and viceversa. Again thanks a lot.

mrojas ( 2014-02-12 16:35:14 +0800 )edit

thanks for your feedback I am glad I could help, and also for sharing information above with sitansu!

cor3000 ( 2014-02-13 01:30:55 +0800 )edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2014-02-10 23:30:37 +0800

Seen: 25 times

Last updated: Feb 13 '14

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