Using Java and COM Together Previous
Previous
Introduction
Introduction
Next
Next

Controlling a Java Applet through Scripting

Web developers can embed ActiveX controls into their Web pages and drive them using Visual Basic Scripting Edition (VBScript). The ActiveX runtime for Java offers developers another option by making Java applets scriptable too. When a Java applet is running in a browser such as Internet Explorer, all the public methods and variables of the applet automatically become available to VBScript or any other language supporting the ActiveX scripting protocol.

As an example, consider a Java applet like the following:

class Buzzer extends Applet
{
	public int pitch;
	public void buzz() 
	{ 
		//play a sound at the specified pitch 
	}
	// other methods
	// ...
}

To include such an applet in your HTML page, simply use the <APPLET> tag, specifying the ID attribute so that you can refer to the applet from the scripting language:

<APPLET CODE="Buzzer.class" ID=doorbell>

You can then define buttons that allow the user to control the applet. For example:

<INPUT TYPE=button VALUE="Higher" NAME="BtnHigher">
<INPUT TYPE=button VALUE="Lower"  NAME="BtnLower">
<INPUT TYPE=button VALUE="Play"   NAME="BtnPlay">

The NAME attribute lets you refer to each button from the scripting language.

In the scripting portion of your HTML page, you can define OnClick handlers for each button, making them manipulate the applet:

<SCRIPT language = "VBScript">
<!--
Sub BtnHigher_OnClick
	document.doorbell.pitch = 880
End Sub

Sub BtnLower_OnClick
	document.doorbell.pitch = 440
End Sub

Sub BtnPlay_OnClick
	document.doorbell.buzz
End Sub
-->
</SCRIPT>

Now, when a user clicks a button on the Web page, the appropriate VB Script handler is invoked, which in turn manipulates the Java applet. Note that when the applet is referenced from VBScript, its name has the identifier "document." prepended to it.

In this way, Java applets can be driven just like ActiveX controls. The script can read and write the public variables of the applet, as well as call its public methods (including passing parameters and reading return values).

Note that only the Applet-derived class is directly accessible from the scripting language. If your Java applet includes other classes that you want to be available to the scripting language, you must define public methods in your Applet-derived class that delegate to those classes.

You can also embed both Java applets and ActiveX controls in the same Web page and use VB Script handlers to connect them. For example, you can read values from an ActiveX control and pass them to a Java applet, or vice versa. (However, in the current implementation, you cannot fire events from a Java applet.)

This automatic scripting capability is provided by the Java support in Internet Explorer. As a result, your script code can manipulate Java applets developed with any tool, not just ones developed with Visual J++.

A more sophisticated way of having Java applets and ActiveX controls communicate is described in Connecting a Java Applet and an OLE Control. This requires knowledge of using a COM object from Java.

For more information about VBScript, see the VBScript Web site at http://www.microsoft.com/vbscript/ (which is accessible using the Web Favorites command on the Help menu).

Top© 1996 Microsoft Corporation. All rights reserved.