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. Here are the rules:
returnValue = AppletID.appletMethod(arg1, arg2, …)
returnValue = AppletID.publicVar
AppletID.publicVar = <value>
returnValue = AppletID.publicVar.publicMethod(arg1, arg2, …)
Where AppletIDis the id attribute of the <applet> tag:
<applet id="sampleApplet" code="..." />
NOTES:
Following is a pretty simple example that demonstrates invoking a Java applet’s method from Javascript code. Here is code of the applet (QuickApplet.java):
import java.awt.*; import javax.swing.*; public class QuickApplet extends JApplet { private JLabel label = new JLabel("This is a Java applet"); public void init() { setLayout(new FlowLayout()); label.setFont(new Font("Arial", Font.BOLD, 18)); label.setForeground(Color.RED); add(label); } public void drawText(String text) { label.setText(text); } }
This applet shows only label and declare a public method drawText(String) which updates the label’s text.
Here is code of the HTML page (QuickAppletTest.html):
<html> <head> <title>Javascript call Applet example</title> </head> <body> <center> <input type="button" value="Call Applet" onclick="callApplet();"/> <br/><br/> <applet id="QuickApplet" code="QuickApplet.class" width="300" height="50" > </applet> </center> </body> <script type="text/javascript"> function callApplet() { var currentTime = new Date(); var time = currentTime.toISOString(); // invoke drawText() method of the applet and pass time string // as argument: QuickApplet.drawText(time); } </script> </html>
This HTML page embeds the above applet and displays a button which will invoke the applet’s method when clicked. The Javascript method callApplet() invokes the drawText() method of the applet and passes a time string as an argument. The applet, in turn, updates the label’s text by the string passed from Javascript code. Here is a screenshot of the page:
Here are some examples of passing arguments to Java applet’s methods from Javascript code.
NOTES:
Code in Java applet:
public int sum(int x, int y) { return (x + y); }
var sum = sampleApplet.sum(10, 20);
Code in Java applet:
public String changeCase(String text) { return text.toUpperCase(); }
var returnText = sampleApplet.changeCase("code java");
var returnText = sampleApplet.changeCase(123456);
Code in Java applet:
public boolean toggleVisible(boolean visible) { return !visible; }
var booleanReturn = sampleApplet.toggleVisible(true);
var booleanReturn = sampleApplet.toggleVisible(""); // return true var booleanReturn = sampleApplet.toggleVisible("visible"); // return false
Code in Java applet:
public int sum(int[] numbers) { int sum = 0; for (int i = 0 ; i < numbers.length; i++) { sum += numbers[i]; } return sum; }
Code in Javascript:
var numbers = new Array(); numbers[0] = 10; numbers[1] = 20; numbers[2] = 30; var sum = sampleApplet.sum(numbers);
Javascript code can access only public variables (both instance and static) declared in the applet. If the variable is of type Object, we can invoke methods on the returned object (and pass arguments) like the above section.
Code in Java applet:
public int number;
sampleApplet.number = 10;
Code in Java applet:
public String email;
Code in Javascript:
var email = sampleApplet.email; email = email.substring(5, 18); email = "info@codejava.net"; email = email.toUpperCase();
In this example, we obtain a String object from the applet and call String’s substring(int, int) and toUpperCase() methods. The similar can be applied for other Object types.
Other Java Applet Tutorials: