Using a Java COM Class

If I wanted to use FormatNumbers in a Java program, I would use it just as I did before I made it available as an ActiveX component. The FormatTest program from the beginning of this chapter will work correctly, whether FormatNumbers is a plain Java class or a class extended by the ActiveX Component Wizard. However, a Java program can also access FormatNumbers via its ActiveX interface, as shown in this example program, FMTest (which you can find in the \ActiveVJ\Chap07\FMTest directory on the companion disc).

import java.applet.*;
import java.awt.*;
import formatnumberslib.*;

//
//
// FMTest
//
//
public class FMTest extends Applet
{
    public FMTest()
    {
    }

    public String getAppletInfo()
    {
        return "Name: FMTest\r\n" +
               "Author: Scott Robert Ladd\r\n" +
               "Created with Microsoft Visual J++ " +
               "Version 1.1";
    }

    public void init()
    {
        resize(700, 250);
    }

    public void destroy()
    {
    }

    public void paint(Graphics g)
    {
        IFormatNumbers fm = 
            (IFormatNumbers)new CFormatNumbers();

        String s1 = fm.formatComma(1234567890);
        String s2 = fm.formatText(1230567001);
        fm.setCurrencySymbol(' ');
        String s3 = fm.formatComma(1);
        String s4 = fm.formatText(1000014081);
        String s5 = fm.formatText(-10000);

        g.drawString(s1, 10, 20);
        g.drawString(s2, 10, 40);
        g.drawString(s3, 10, 60);
        g.drawString(s4, 10, 80);
        g.drawString(s5, 10, 100);
    }

    public void start()
    {
    }
    
    public void stop()
    {
    }

In FMTest, I include the interface information for the FormatNumbers component; then I create a CFormatNumbers object, cast the returned pointer to an IFormatNumbers interface, and then proceed to use the interface object as if it were a normal Java object. A C++ program should be able to do the same thing, using the CLSID for FormatNumbers to create an object and access it via its interfaces.

© 1997 by Scott Ladd. All rights reserved.