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.
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
A collapsed view of your project appears in Project Explorer.
A file with the default name of Form1.java has been added to your project.
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
The COM Wrappers dialog box is displayed.
For this scenario, select the Stats COM object.
Visual J++ adds package directories and class wrappers for each of the COM objects contained in the COM DLL that you selected.
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
The text editor displays the source code for Form1.
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
.
After you have incorporated calls to the COM objects methods, you build and run the project.
To build and run the project
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.)