How to call a method declared in an applet from JavaScript
Asked Answered
O

1

5

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?

Olson answered 24/12, 2012 at 4:10 Comment(1)
The applet will be a component in a version control system. A separate web service copies the file to the clients computer when they "check-out" code but I want to automatically open it in their default text editor at the same time. If you can think of an easier way to open the text file I'm all ears.Olson
A
6

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);
    }
}
Aircraft answered 24/12, 2012 at 4:14 Comment(6)
And since when can you do document.Applet_ID?Semitropical
@Ian, I believe, now you would be happy with my update ?? Or still I need more improvement ??Aircraft
@Olson are you sure, you are getting that error ?? Your .class file in right place ?? please checkAircraft
You can't use document.AppletABC, I'm not sure where you got that from. You can do that when the name of the element is set though, not with ids. document.getElementById("AppletABC") is how you should do it. Also, I don't think you can put <script> next to the <head> element...you have to put it inside of <head> or <body>.Semitropical
I think the issue was I did not have an init function so the applet was not properly starting. Thanks.Olson
@Ian: Actually, you can use the ID; it references the applet tag.Fishing

© 2022 - 2024 — McMap. All rights reserved.