Microsoft SDK for Java

Hosting an ActiveX Control in Java

Microsoft® ActiveX® Controls are Automation-enabled controls that support the IDispatch COM interface, which enables the control to be scripted. These controls can also be dropped into containers, such as Visual Basic.

Hosting an ActiveX control in Java is a four-step process:

  1. Develop or obtain an ActiveX control and its associated type library.

  2. Run a tool called jactivex on the ActiveX control or its type library.

    This tool will emit Java source code wrappers for the IDispatch interface implemented by the ActiveX control. The emitted Java code will include Bean-like code for adding and removing listeners for the events originating from the ActiveX control. Also, the tool produces code to appropriately manage the lifetime of the ActiveX control.

    The source code will contain compiler directive code, similar to that used by J/Direct, of the form:

    /** @com */
  3. Compile this Java source code using a compiler, such as jvc, that can interpret the @com syntax. The compiler will add some attributes to the class file.

  4. Import these classes and use the ActiveX control like any other Bean in Java code. The Microsoft VM will use these attributes in the class file to expose the ActiveX control as a Bean to any Java code. The Microsoft VM provides the necessary bridge between the invoking Java code and the corresponding IDispatch interface in the ActiveX control. Note that this only works with virtual machines that understand these attributes.

    This is best illustrated with an example. The following sample uses the Microsoft Calendar ActiveX control that ships with Microsoft Office. Running the tool jactivex on the Calendar control produces Java source classes, which are then compiled into the calendar classes imported below. A Java programmer would use the control with code similar to the following:

    import java.awt.*;
    import com.ms.activex.*;
    import calendar.*;  // Classes from JActiveX when run on Calendar control or typelib
    
    public class UseCalendar extends Frame implements ActiveXControlListener
    {
           Calendar c;
           public UseCalendar()
           {
            c = new Calendar();
    
            c.addActiveXControlListener(this);
            add(c);
           }
    
           // Implementation of ActiveXControlListener interface method(s)
           public void controlCreated(Object o);
           {
            if (o instanceof Calendar)
            {
                    c.today();
            }
           }
    }
    

Notice that the control is manipulated like any other Bean. The constructor code creates the Bean, registers a listener (in this case, one defined by the ActiveXControlListener interface), and then adds itself to the layout. The single method of the ActiveXControlListener interface is controlCreated. When the control is created, the Microsoft VM fires an event defined by the ActiveXControlListener interface and, if the ActiveX control supports events as this example does, the corresponding listener method on the Bean is called. The code necessary to sink the event to the corresponding listener is provided automatically by the Microsoft VM.

The result is that when the Calendar control is created, and the controlCreated method is called, which displays the month corresponding to the current date.

© 1999 Microsoft Corporation. All rights reserved. Terms of use.