Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How can I call a function when a specific event occurs in my java Applet ?

In my Javascript I has the following code, that always return a empty value.

$(function () {
    alert(document.applets[0].returnClientId());
});

I need to call this alert, just when a specific method is executed. To be more specific, the method called identify, that exists in my java Applet.

The identify method, exists in my Util.class, that don't extends from JApplet. My FormMain.class extends from JApplet and I call some methods (including the identify method) from this Util.class.

UPDATE: My Java Code

public String getClientid() {
    return clientid;
}

public void setClientid(String clientid) {
    this.clientid = clientid;
}

public String returnClientId() {
    return getClientid();
}

public void identify() {
    try {
        fingerprintSDK.prepareForIdentification(this.template);

        ResultSet rs = identifyStmt.executeQuery();

        while (rs.next()) {         
            byte[] templateBuffer = rs.getBytes("template");

            Template referenceTemplate = new Template(templateBuffer);

            boolean matched = fingerprintSDK.identify(referenceTemplate);

            if (matched) {
            // ui is my FormMain instance
                ui.showImage(GrFingerJava.getBiometricImage(template,
                        fingerprint, fingerprintSDK));

                ui.writeLog("Found. Client = "
                        + rs.getString("Name"));

                ui.setClienteid(rs.getString("Cliente_Id"));

                ui.disableTemplate();                   
                return;
            }
        }

        ui.writeLog("Not Found.");
        ui.enableTemplate();
    } catch (SQLException e) {

        ui.writeLog(e.getMessage());
    } catch (GrFingerJavaException e) {
        ui.writeLog(e.getMessage());
    }
}

The Identify method is executed just when my User put the finger in the biometric device.

Someone has some idea ?

share|improve this question
1  
These are the docs. We can't reproduce your code, though. Is returnClientId() declared public? Does it run without errors? –  Raffaele Jul 25 '12 at 15:42
    
@Raffaele All method are public –  Lucas_Santos Jul 25 '12 at 15:51
    
Check on the Java side if the String is actually empty (BTW, why is there getClientid() and returnClientId()???) –  Raffaele Jul 25 '12 at 15:58
    
@Raffaele I know, I can just return clientid. My String is actually empty, I fill this String just when my Identify method is executed. –  Lucas_Santos Jul 25 '12 at 15:59
1  
What a bad question! Please, review it! If I understand the flow correctly, when the applet is loaded the client id is blank. It's set to some non-blank when Util.identify() is called (but you don't tell how this happens). The problem I see here is that the Javascript alert executes on DOM loaded (this is what jquery does when you write $(function() {})), when the string can be null. And in fact it is –  Raffaele Jul 25 '12 at 16:16

1 Answer 1

up vote 0 down vote accepted

Suppose you have the following JS function

function foo(client) {
    alert(client);
}

you'll modify your Utils.java as follows

public void identify() {
    // the rest of your code
    String id = rs.getString("Cliente_Id");
    ui.setClienteid(id);
    // call the Javascript function
    JSObject.getWindow(ui).eval(String.format("foo(%s)", id));
}

To compile the code using netscape.javascript.* package you need the $JAVA_HOME/jre/lib/plugin.jar. See here

In order to call JavaScript, Java code uses the netscape.javascript.JSObject and netscape.javascript.JSException classes. Since the release of Java 2, Standard Edition version 1.4, these classes are provided in the jar file jre/lib/plugin.jar within either the Java Development Kit or Java Runtime Environment. If you reference these JavaScript classes, you will need to add plugin.jar to your compilation classpath. This can be done through your Java IDE, if you use one, or by passing the -classpath command-line argument to the Java compiler javac.

At run-time, the Java Plug-In automatically makes these classes available to applets, so no changes to the applet or how it is set up are necessary.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.