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:

    • Invoking a public method defined in the applet:

returnValue = AppletID.appletMethod(arg1, arg2, …)

    • Accessing a public variable defined in the applet:

returnValue = AppletID.publicVar

    • Setting value for the variable:

AppletID.publicVar = <value>

    • Invoking a public method of the variable of type Object:

returnValue = AppletID.publicVar.publicMethod(arg1, arg2, …)

Where AppletIDis the id attribute of the <applet> tag:

<applet id="sampleApplet" code="..." />

NOTES:

    • Javascript code can access only public methods and variables.
    • Both static or instance methods/variables can be accessed.
 

1. Javascript call Java Applet Quick Example

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:

Javascript to Applet page



 

2. Invoking Java applet’s methods

Here are some examples of passing arguments to Java applet’s methods from Javascript code.

NOTES:

    • Because variables in Javascript are untyped, so they will be converted to Java equivalents based on signature of Java methods.
    • Return values from Java methods are converted to closest Javascript equivalents (for primitive types) or Javascript wrapper object (for Object types).
  • Passing numeric arguments:Code in Java applet:

    public int sum(int x, int y) {
    	return (x + y);
    }

    Code in Javascript:
    var sum = sampleApplet.sum(10, 20);
     

  • Passing String arguments:Code in Java applet:

    public String changeCase(String text) {
    	return text.toUpperCase();
    }

    Code in Javascript:
    var returnText = sampleApplet.changeCase("code java");

    Numbers are automatically converted to String:
    var returnText = sampleApplet.changeCase(123456);
     
  • Passing boolean arguments:Code in Java applet:

    public boolean toggleVisible(boolean visible) {
    	return !visible;
    }

    Code in Javascript:
    var booleanReturn = sampleApplet.toggleVisible(true);

    Empty string is converted to false, non-empty is converted to true:
    var booleanReturn = sampleApplet.toggleVisible(""); // return true
    var booleanReturn = sampleApplet.toggleVisible("visible"); // return false
     
  • Passing array arguments:

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);
 

3. Accessing Java applet’s variables

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.

  • Access primitive variables:Code in Java applet:

    public int number;

    Code in Javascript:
    sampleApplet.number = 10;
     

  • Access Object variables:

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:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment

   


Comments 

#3Bob Hopp2015-04-23 11:00
Hi,
It's 2015 and I'm still trying to embed a java applet! There doesn't seem to be a lot of expertise out there these days (or nobody wants to admit to knowing).

Everything seems to work, expect occasionally, under certain circumstance a call into the applet does not return in a timely fashion and causes the entire browser to hang. is there a way to get the call to throw an exception after some time if it does not a response?

Thanks
Bob
Quote
#2Nam2014-09-25 13:02
What don't you understand?
Do you want a working sample which can be downloaded?
Quote
#1nallib tala2014-09-25 09:59
i dont get it...
wheres the java file. is a jar ?, a class?

please explain us :D
Quote