Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a method declared in an applet from JavaScript

I am trying to make a basic Java applet to open a file on the client's computer for them. I would like to call the openFile function in the Java applet below via JavaScript.

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

import javax.swing.JApplet;

public class Test extends JApplet {
    public void openFile(String filePath) {
        File f = new File(filePath);

        try {
            Desktop.getDesktop().open(f);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In between the body tags of my webpage I have the following:

<applet code="Test.class" height="0" width="0"></applet>

<script type="text/javascript">
    document.applets[0].openFile("C:\\test.log");
</script>

When I load the page I get the error:

TypeError: Object # has no method 'openFile'

What do I need to do to fix this error and get the applet working?

like image 588
Daniel Avatar asked Dec 24 '12 04:12

Daniel


People also ask

Can you call a Java applet using a JavaScript function?

Javascript can talk to Java applet in the same HTML page through LiveConnect technology. We can write Javascript code to invoke methods (including arguments) and access objects which are exposed by the applet.

Which method is called by applet?

The applet life cycle can be defined as the process of how the object is created, started, stopped, and destroyed during the entire execution of its application. It basically has five core methods namely init(), start(), stop(), paint() and destroy(). These methods are invoked by the browser to execute.

Can we develop applet using JavaScript?

The JavaScript code can use the applet id as a reference to the applet object and invoke the applet's methods. In the example shown next, the JavaScript code sets the applet's public member variables, invokes public methods, and retrieves a reference to another object referenced by the applet ( Calculator ).


1 Answers

Use:

<script src=
  "http://www.java.com/js/deployJava.js"></script>

<script>
    <!-- The applet id can be used to get a reference to
    the applet object -->
    var attributes = { id:'mathApplet',
        code:'jstojava.MathApplet',  width:1, height:1};
    var parameters = {jnlp_href: 'math-applet.jnlp'};
    deployJava.runApplet(attributes, parameters, '1.6');
</script>

Reference: Invoking Applet Methods From JavaScript

JavaScript is allowed to directly call the applet’s public methods or public variables. JavaScript considers the embedded applet as an object. By providing the applet with an ID, JavaScript can access it with:

    document.Applet_ID.Applet_Method()

And you can use this,

File MyApplet.html

<html>
<head>
    <script language="Javascript">
        function accessAppletMethod()
        {
            document.getElementById("AppletABC").appendText("Applet Method");
        }
    </script>

    <title>Testing</title>
</head>

<body onload="accessAppletMethod()">

    <h1>Javascript acess Applet method</h1>

    <applet width=300 height=100 id="AppletABC"
        code="JavaScriptToJava.class">
    </applet>
</body>

</html>

File JavaScriptToJava.java

import java.applet.Applet;
import java.awt.FlowLayout;
import java.awt.TextArea;

public class JavaScriptToJava extends Applet{

    TextArea textBox;

    public void init(){
        setLayout(new FlowLayout());
        textBox = new TextArea(5, 40);
        add(textBox);
    }

    public void appendText(String text){
        textBox.append(text);
    }
}
like image 109
Ravi Avatar answered Oct 12 '22 13:10

Ravi