Using Java and COM Together |
![]() Previous |
![]() Introduction |
![]() Next |
You can have a Java applet interact with an OLE control that resides on the same HTML page, using a combination of the techniques described in the topics Controlling a Java Applet through Scripting and Using a COM object from Java.
There are two distinct ways of connecting a Java applet with an OLE control:
This allows the applet to respond to events fired by the control.
This is done by passing the control to the Java applet. This requires work in both Java and VBScript:
It is common to use both types of connection in order to establish two-way communication between the applet and the control.
In this release, a Java applet cannot directly receive events fired by an OLE control. However, you can define VBScript methods that handle events fired by the control. These methods can then, in turn, call methods on the Java applet, as described in Controlling a Java Applet through Scripting.
For example, consider an HTML page containing both an OLE control and a Java applet:
<OBJECT ID=Outline WIDTH=100 HEIGHT=100 CLASSID="CLSID:BE4F3AC5-AEC9-101A-947B-00DD010F7B46"> </OBJECT> ... <APPLET CODE="OutlineUser.class" ID=user>
This page contains an outline control and an applet that uses the outline control.
You can then define event handlers in VBScript for the events fired by the control. For example:
<SCRIPT language=VBScript> <!-- sub Outline_Collapse document.user.handleCollapse end sub sub Outline_Expand document.user.handleExpand end sub --> </SCRIPT>
In this way, VBScript acts as an intermediary between the OLE control and the Java applet, receiving events from the former and calling methods on the latter.
In this release, you cannot embed an ActiveX control directly into a Java applet the way you embed an AWT (Abstract Window Toolkit) control. However, you can have your HTML page pass an OLE control as a parameter to a Java method, and have your Java applet use it that way.
In order for your Java applet to know how to use an OLE control, you must first use the JavaTLB tool to import the OLE control, that is, to generate .CLASS files that expose the control's functionality to Java programs.
In your Java source, you should use one or more import statements to allow convenient references to the package associated with the OLE control. For example, if you had run the JavaTLB tool on the MSOUTL32.OCX control, you would use statements like this:
import msoutl32.*; import olepro32.*;
There are two import statements because the type library inside MSOUTL32.OCX contains an importlib statement.
Then in your Java applet, define a public method that takes an Object as a parameter. For example:
public OutlineUser extends Applet { // IOutlineCtrl interface defined in package MSOUTL32 IOutlineCtrl m_outlineCtrl; public void setCtrl(Object oc) { m_outlineCtrl = (IOutlineCtrl)oc; } // other methods // ... }
The setCtrl method casts the object to the IOutlineCtrl interface and stores a reference to that object. Because this method is public, it's accessible to the VBScript portion of the HTML page in which the applet resides. A VBScript function will use this function to pass an OLE control to the applet.
Once your Java applet has a reference to the object, in the form of an appropriate interface, you can call methods on the outline control. For example:
public String getText() { return m_outlineCtrl.getText(); }
This Java method retrieves a property from the outline control.
Note that only trusted Java applets can make use of OLE controls in this manner.
Preparing the Java applet to receive the control is only half the work needed to let the applet drive an OLE control. The other half is passing the control from VBScript.
First insert the OLE control on your HTML page using a tag like the following:
<OBJECT ID=Outline WIDTH=100 HEIGHT=100 CLASSID="CLSID:0713E8A2-850A-101B-AFC0-4210102A8DA7"> </OBJECT>
This tag embeds an instance of the a outline control (which is implemented by MSOUTL32.OCX). The NAME attribute lets you refer to the button from the scripting language. You can use the ActiveX Control Pad to insert controls without having to enter the CLSID manually.
Then insert the Java applet on the HTML page using a tag like the following:
<APPLET CODE="OutlineUser.class" ID=user>
The ID attribute lets you refer to the applet from the scripting language.
In the VBScript portion of your HTML page, define a window_onLoad function that passes a reference to the OLE control to the Java applet. For example:
<SCRIPT language="VBScript"> <!-- sub window_onLoad document.user.SetCtrl Outline end sub --> </SCRIPT>
The window_onLoad function is called as soon as the HTML page has finished loading. The above example causes a reference to the outline control to be passed to the Java applet's setCtrl method, as defined in Receiving a Control as a Parameter in Java. From that point forward, the Java applet can manipulate the outline control.
By both passing the control to the Java applet, and forwarding events from the control to the applet, you establish two-way communication between the control and the applet.