Importing COM Objects

See Also

COM objects provide a great way to encapsulate functionality and reuse it across multiple applications. You can use COM objects to expose specific functionality within your application to use in other applications or to create a set of routines that you want to use in more than one application. Because COM is not language-specific, COM objects can be easily integrated into applications developed in a variety of programming languages. You can use Visual J++ to view and import COM objects that are registered on your system. During the import process, Visual J++ creates class wrappers so that you can access COM objects just as you would other Java objects.

In this scenario, you import a COM object that was built in the Building COM Objects topic. You will learn how to:

Note   This topic assumes that you have created the COM object in Building COM Objects and that you have closed any open projects.

Creating a Project

When importing a COM object, Visual J++ creates directories within your project and adds the class wrappers to access the COM object in those directories. To import a COM object, you must have a valid Java project.

To create a project

  1. On the File menu, click New Project.

  2. On the New tab, expand the Visual J++ Projects folder, click Applications, and select the Windows Application icon.

  3. In the Name box, type a name for your project.

  4. In the Location box, type the path where you want to save your project, or click Browse to navigate to the folder.

  5. Click Open.

    A collapsed view of your project appears in Project Explorer.

  6. In Project Explorer, expand the project node.

    A file with the default name of Form1.java has been added to your project.

Importing a COM Object

After you have created a project, you can then import COM objects to that project. A COM object must be imported for each project that is to access it. When you import a COM object into a project, Visual J++ creates class wrappers that provide the interface for accessing the COM object. These class wrappers are added to packages  in your project directory. (Depending on the number of objects stored in the COM DLL, Visual J++ can create multiple packages.)

Note   To access a specific COM object from another project, you can avoid wrapping the object for each project by placing the COM wrapper classes in the Classpath.

To import a COM object

  1. On the Project menu, click Add COM Wrapper.

    The COM Wrappers dialog box is displayed.

  2. In the list that contains COM DLLs and type libraries that are registered on your computer, select the name of the COM object that you want to import.

    For this scenario, select the Stats COM object.

  3. Click OK.

    Visual J++ adds package directories and class wrappers for each of the COM objects contained in the COM DLL that you selected.

Adding Code to Access the COM Object

After Visual J++ has created the class wrappers, you can then write code to access the methods of the COM object. For this scenario, you add code to your project's form constructor. This code creates an instance of the COM object and make calls to the two methods of the COM object. The methods are called with hard-coded values, and their results are displayed in the Output window.

To add code to the constructor of the form

  1. In Project Explorer, right-click Form1 and then click View Code.

    The text editor displays the source code for Form1.

  2. Inside the Form1 constructor below the call to initForm, add the following lines of code:
    statisticsobject.Stats stats = new statisticsobject.Stats();
    System.out.println("Our team has a winning percentage of " +
          stats.winLossPercentage(10, 4));
    System.out.println("Our goaltender has a Goals Against Average of " +
          stats.goalsAgainstAverage(12, 15));

This code creates an instance of the Stats COM object. The reference to statisticsobject before the Stats object is the name of the package where the class wrappers for Stats were created. After the instance of Stats is defined, the code calls its winLossPercentage and goalsAgainstAverage methods with hard coded values and sends the results to the Output window through a call the System.out.println.

Building and Running the Project

After you have incorporated calls to the COM objects methods, you build and run the project.

To build and run the project

  1. On the Build menu, click Build. (If you receive any compilation errors or messages, correct the errors and rebuild your project.)

  2. To run the form, click Start on the Debug menu.

After the project starts, you can see the output from the calls to the Stats COM object in the Output window. (To display the Output window, on the View menu, click Other Windows, and then click Output.)