Archived     (close)   (home)
GalaSoft Laurent Bugnion This page is not maintained anymore, and has been archived. The information on this page must be taken with care, because it might not be valid anymore.
For more information, see this page or contact me
With the compliments of

Visit GalaSoft

Back to JavaScript Consulting index

Click here to see the Java code.

Click here to read important points to observe when dealing with LiveConnect.

Does this work in the Java plug-in?
Click here to know


Do you have another question which not handled here?
Click here...
Communication Java <--> JavaScript
(LiveConnect)


Different users asked about LiveConnect.
Here is a basic example involving communication between Java and JavaScript, with an applet compiled using JDK 1.0.2, running in the standard JVM.

Following cases are handled
A: Java -> JavaScript

1) Java notifies JavaScript when the applet is started.
2) Java notifies JavaScript when a Java button is clicked.
3) Java accesses a form element directly in the HTML
4) Java accesses a form element in the HTML through a JS function with arguments
5) Java uses alert() in JavaScript
B: JavaScript -> Java
6) JavaScript calls a public method in Java with an argument (int)
7) JavaScript calls a public method in Java with an argument (string)

Your browser does not support Java...




This textfield is accessed by the applet through a JavaScript function


Copy the value of this text field to the applet's textfield on click of the button

<SCRIPT LANGUAGE="JavaScript"> <!-- /* Script by GalaSoft Laurent Bugnion */ /* 06.09.2000 */ /* www.galasoft.ch */ // This variable stores the number of time that the JS button // has been clicked var iJSCounter = 0; // This function passes the value to the applet by // calling a public method function callApplet() { var theApplet = document.TestJSObject; theApplet.jSButtonClicked( ++iJSCounter ); } // This function calls a method in the applet // with a string parameter function copyText( aText ) { var theApplet = document.TestJSObject; theApplet.receiveText( aText ); } // This function is called from the applet function copyValueFromApplet( theTextValue ) { document.myForm.textOutput2.value = theTextValue; } //--> </SCRIPT> <APPLET ARCHIVE = "TestJSObject.jar" CODEBASE = "." CODE = "galasoftLB.pTestJSObject.TestJSObject.class" NAME = "TestJSObject" WIDTH = 505 HEIGHT = 70 HSPACE = 0 VSPACE = 0 ALIGN = middle MAYSCRIPT> Your browser does not support Java... </APPLET> <FORM NAME="myForm"> <INPUT NAME="textOutput" TYPE="text" VALUE="(void)" SIZE="45" ONFOCUS="blur();"> <BR><BR> <INPUT NAME="buttonInput" TYPE="button" VALUE="Call the applet" ONCLICK="callApplet();"> <BR><BR> This textfield is accessed by the applet through a JavaScript function <BR> <INPUT NAME="textOutput2" TYPE="text" VALUE="" SIZE="45" ONFOCUS="blur();"> <BR><BR> Copy the value of this text field to the applet's textfield on click of the button <BR> <INPUT NAME="textInput1" TYPE="text" VALUE="Enter a text here" SIZE="45"> <BR> <INPUT NAME="buttonInput2" TYPE="button" VALUE="Copy the text" ONCLICK="copyText(this.form.textInput1.value);"> </FORM>