Controlling an Applet Using Scripting
 In this topic

*Controlling a Java Applet Through Scripting

*Firing Events from an Java Applet

 

Articles    Welcome!
Controlling an Applet Using Scripting     Welcome!

 


Controlling an Applet Using Scripting

Web developers can embed Microsoft® ActiveX® Controls into their Web pages and drive them using Microsoft® Visual Basic® Scripting Edition (VBScript). The Microsoft Win32 VM for Java (Microsoft VM) offers developers another option by making Java applets scriptable, too. When a Java applet is running in a browser, such as Microsoft® 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.

Controlling a Java Applet Through Scripting

Consider the following fragment from a Java applet.



	class Buzzer extends Applet

	{

		public int pitch;

		public void buzz() 

		{ 

			//Play a sound at the specified pitch. 
			...   

		}

 ...

	}

To include this applet in your HTML page, use the <APPLET> tag, specifying the ID attribute. This enables you to refer to the applet from the scripting language, as shown in the following example:



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

You can then define buttons that enable the user to control the applet, as shown in the following 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, as follows:



	<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 VBScript 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 attached to it.

Using scripting, 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). However, only classes derived from java.Applet class are directly accessible from the scripting language. If your Java applet includes other classes that you want to make available to the scripting language, you must define public methods in your Applet subclass that delegate to those classes.

You can also embed both Java applets and ActiveX controls in the same Web page and use VBScript handlers to connect them. For example, you can read values from an ActiveX control and pass them to a Java applet, or vice versa. You can even fire events from a Java applet. This automatic scripting capability is provided by the Microsoft VM for Java. As a result, your script code can manipulate Java applets developed with any tool, not just ones developed with Microsoft® Visual J++™.

Firing Events from an Java Applet

Applets in Internet Explorer 4.0 can fire events to scripts. To do so, use the <applet> tag and specify a <param name=FireScriptEvents value=True> tag, as shown in the following example.

A look at an example taken from the AFCBean sample in the SDK for Java may help to demonstrate how this works.

The AFCBean class in AFCBean.java contains a handleEvent method that fires an event called itemSelected.



    /**

     *  When a item is selected, an action is performed. The LIST_SELECT

     *  event is parsed and an event called itemSelected is fired.

     */

     public boolean handleEvent(Event e)

     {

        if ((e.target instanceof UIList) && (e.id == Event.LIST_SELECT))

        {

            AFCBeanListener listener[];



            synchronized (m_listeners)

            {

                listener = new AFCBeanListener[m_listeners.size()];

                m_listeners.copyInto(listener);

            }



            for (int i=0; i<listener.length; i++)

                listener[i].itemSelected(getSelectedItem());

        }



        return super.handleEvent(e);

    }

In the AFCBean.html file, the following <applet> tag declares that the applet can fire events that will be handled by the script.



    <applet code="AFCBean.class" name="AFCBean" width="300" height="300">

        <param name="FiresScriptEvents" value="true">

    </applet>

The following JavaScript then sinks the event.



<script language="JavaScript" for="AFCBean" event="itemSelected(s)">

    txtSelectedText.value = s;

</script>

Finally, the selected string is displayed in the table on the HTML page.



    <tr>

        <td>Sinks the event itemSelected and displays the string.</td>

        <td><input name="txtSelectedText"></td>

 </tr>

</e>

When the <object> tag is supported by the Microsoft VM, it will assume the JDK 1.1 event model, so the FireScriptEvents parameter will not be required when using this tag.

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

Top © 1998 Microsoft Corporation. All rights reserved. Terms of use.