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:

    1. The steps to invoke Javascript function
    2. A complete example
    3. Passing numeric arguments example
    4. Passing String arguments example
    5. Passing boolean arguments example
    6. Passing array argument example

1. The steps to invoke Javascript function

The typical steps to call a Javascript function from a Java applet are as follows:

NOTES:

2. A complete Java applet invoking Javascript method example

The following example creates an applet looks like this:

simple applet call javascript

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:



sum message dialog

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>
 

3. Passing numeric arguments example

Javascript function:

function multiply(x, y) {
	return x * y;
} 
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

 

4. Passing String arguments example

5. Passing boolean arguments example

jsObj.call("setTableVisible", new Boolean[] {false});
 

6. Passing array argument example

Other Java Applet Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.