A Java application cannot create an ActiveX component itself, nor can it reference a COM object directly. You must create the ActiveX control within the HTML document and then pass a reference to that object’s interface to your Java code. Java can talk to an ActiveX control only through one of the control’s COM interfaces. This is different from the way Java creates and manages the visual controls from the Abstract Window Toolkit, and it may take some getting used to. Here’s an example.
Let’s say I have created a Super Label object on my web page with the following piece of HTML.
<OBJECT ID="AXLabel" WIDTH=137 HEIGHT=137
CLASSID="CLSID:99B42120-6EC7-11CF-A6C7-00AA00A47DD2">
<PARAM NAME="_ExtentX" VALUE="3625">
<PARAM NAME="_ExtentY" VALUE="3625">
<PARAM NAME="Caption" VALUE="Rotate!">
<PARAM NAME="Angle" VALUE="0">
<PARAM NAME="Alignment" VALUE="4">
<PARAM NAME="Mode" VALUE="1">
<PARAM NAME="FillStyle" VALUE="0">
<PARAM NAME="ForeColor" VALUE="#000000">
<PARAM NAME="BackColor" VALUE="#B0C4C8">
<PARAM NAME="FontName" VALUE="Arial">
<PARAM NAME="FontSize" VALUE="24">
<PARAM NAME="FontItalic" VALUE="0">
<PARAM NAME="FontBold" VALUE="1">
<PARAM NAME="FontUnderline" VALUE="0">
<PARAM NAME="FontStrikeout" VALUE="0">
<PARAM NAME="TopPoints" VALUE="0">
<PARAM NAME="BotPoints" VALUE="0">
</OBJECT>
In my Java class, I define a reference to an IIeLabel interface, which I set via the public setLabel method.
public class LabelHandler extends Applet
{
IIeLabel m_label;
public void setLabel(Object ctl)
{
m_label = (IIeLabel)ctl;
}
}
Then, back in the HTML document, I add a piece of script that calls setLabel with a reference to the Super Label control.
<SCRIPT language="VBScript">
<!--
sub window_onLoad
document.LabelHandler.setLabel AXLabel
end sub
-->
</SCRIPT>
Now the Java program can communicate with the ActiveX label through the COM interface. The only problem with this technique is that you could, by mistake, pass the wrong reference to the setLabel method since the Object type can’t perform any type checking.