Java applet can talk to Javascript through the technology called LiveConnect. In this article, we are going to show you how to write Java code to invoke Javascript function from within Java applet (include passing arguments of various data types and receiving return values).
Table of content:
The typical steps to call a Javascript function from a Java applet are as follows:
JSObject jsObj = JSObject.getWindow(applet);
Where applet is the instance of the applet class which extends from java.applet.Applet class. The JSObject class defines principal methods which allow Java applet accessing Javascript variables and methods.
Object result = jsObj.call("add", new Integer[] {17, 28});The call() method’s first parameter is the name of Javascript method, and the second one is an array of Object for the Javascript method’s parameters. The call() method always returns an Object as return value of the invoked method (null if the Javascript method does not return anything).
NOTES:
The following example creates an applet looks like this:
This applet shows two text fields which allow users entering two numbers. On clicking the Sum button, the applet will call a Javascript function to calculate the sum and show result in a message dialog like this:
Code of the applet (SimpleApplet.java):
import java.awt.*; import java.awt.event.*; import javax.swing.*; import netscape.javascript.*; /** * A simple Java applet that demonstrates invoking a Javascript method */ public class SimpleApplet extends JApplet { private JTextField textA = new JTextField(5); private JTextField textB = new JTextField(5); private JButton button = new JButton("Sum"); public void init() { // constructs the GUI getContentPane().setLayout(new FlowLayout()); getContentPane().add(textA); getContentPane().add(textB); getContentPane().add(button); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { buttonActionPerformed(evt); } }); } public void buttonActionPerformed(ActionEvent evt) { int numberA = Integer.parseInt(textA.getText()); int numberB = Integer.parseInt(textB.getText()); try { JSObject jsObj = JSObject.getWindow(this); // calls Javascript method and gets return value Object result = jsObj.call("add", new Integer[] {numberA, numberB}); JOptionPane.showMessageDialog(this, "Sum is " + result.toString()); } catch (JSException ex) { ex.printStackTrace(); } } }
Compile the applet as follows:
javac -cp JDK_HOME\jre\lib\plugin.jar SimpleApplet.java
NOTE: Replace JDK_HOMEby the real path on your system.
Code of the HTML page (TestSimpleApplet.html):
<html> <head> <title>Test applet calling Javascript function</title> </head> <body> <center> <applet id="simpleApplet" code="SimpleApplet.class" width="200" height="50" > </applet> </center> </body> <script type="text/javascript"> function add(a, b) { return (a + b); } </script> </html>
Javascript function:
function multiply(x, y) { return x * y; }
Object returnObj = jsObj.call("multiply", new Integer[] {81, 92}); Double result = (Double) returnObj; System.out.println("result is: " + result);
Output: result is 7452.0
Object returnObj = jsObj.call("multiply", new Double[] {81.2, 92.3}); Double result = (Double) returnObj; System.out.println("result is: " + result);
Output: result is 7494.76
function getFullname(first_name, last_name) { return "Hello " + first_name + " " + last_name; }
String returnValue = (String) jsObj.call("getFullname", new String[] {"Peter", "Smith"}); System.out.println(returnValue);
function setTableVisible(visible) { var tableDiv = document.getElementById("tableDiv"); tableDiv.style.visibility = visible ? "visible" : "hidden"; }
jsObj.call("setTableVisible", new Boolean[] {false});
function sumArray(array_numbers) { var total = 0; var i = 0; for (i = 0; i < array_numbers.length; i++) { total += array_numbers[i]; } return total; }
int[] numbers = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Object returnObj = jsObj.call("sumArray", new Object[] {numbers}); Double result = (Double) returnObj; System.out.println("result is: " + result);
Other Java Applet Tutorials: