- Using Visual J++ -

Chapter 17

ActiveX and COM Technologies


In this chapter, you will learn what ActiveX and COM technology is all about. Actually, ActiveX and COM require no new programming statements or advanced technical fortitude. You will see that you can use an ActiveX control, such as a Visual Basic OCX control, that you purchased for Visual Basic applications, inside your Visual J++ applets. To the Java language, an ActiveX control looks and acts just like other objects inside the applet.

The ActiveX and COM technologies let you harness pre-written routines and controls inside your own applets. You'll not really write programs as much as you'll piece together ActiveX controls that do what you want them to do.

What Is ActiveX?

According to Microsoft, the ActiveX technology is the most exciting thing to hit the world since sliced bread. Generally, Microsoft feels this way about all its products! Of course, one has to admit that Microsoft does seem to produce some of the best software in the world (just look at Visual J++!) so Microsoft's excitement seems warranted in many instances.

The ActiveX technology produces controls, such as command buttons and scroll bars (and even much more exciting controls), both on the Web as well as the desktop. In other words, instead of using controls such as the common Visual Basic VBX, OCX, and OLE controls for desktop applications but then using AWT components for Web applications, software developers can create ActiveX controls that work everywhere. Visual J++ lets you create ActiveX controls using Visual J++'s Java language implementation.

The Common Object Model (COM) technology includes all OLE (Object Linking and Embedding), OLE's internal automation technology, as well as new standards for sharing objects between programs. OLE has been around since 1990, and therefore, you can use all existing OLE tools from inside your Visual J++ program because of Visual J++'s support for COM includes, by default, OLE support as well.

 
If you already know something about ActiveX, you might think that a developer must use C++ to create and use them. Visual J++ integrates its Java language with C++ routines to get around the C++ requirement for ActiveX control calls.

ActiveX controls go way beyond controls that mimic (and improve upon) AWT components. ActiveX controls manipulate and control sound, video, hardware, and even virtual reality technology. As hardware vendors develop new hardware technology, such as new storage and video devices, you'll have easy I/O access to that new technology through your programs simply by using hardware vendor-supplied ActiveX controls.

Perhaps the biggest advantage of ActiveX controls is that multiple ActiveX controls on a Web page can communicate with each other and pass data back and forth. This two-way communication between controls virtually eliminates tedious programming methods that you would otherwise have to write. By utilizing the COM technology, these ActiveX controls communicate using a standard I/O model.

Visual J++ makes the COM object (in this case, the ActiveX control although there exist many other kinds of COM objects) look to your Visual J++ applet like a pre-defined class. A COM object has properties, such as a maximum and minimum value if the control requires limited values, and your Visual J++ program will access those values as if the values were class data members. In reality, however, you know that the values came to you through the COM object's COM-defined interfaces (called typelibs meaning COM's type libraries).

 
Here's just a sampling of the COM's power: Applications that support the standard COM interface, such as Excel, let you utilize pre-defined functionality even though you have no access to the software's source code. In other words, you'll be able to insert a working Excel worksheet right in the middle of a Web applet without writing a single line of code!

Lots of non-Microsoft companies (even their competitors!) support or plan to support ActiveX technology. Borland, Oracle, Sun, Sybase, and Symantec have all agreed to offer partial or full ActiveX support in their products. Obviously, the industry takes ActiveX as seriously as Microsoft does. Perhaps the reason for their interest is that an ActiveX control works seamlessly in Visual C++, Visual Basic, and even Borland's Delphi and Borland C++ technologies with no modifications. In addition to software vendor support, ActiveX also works on multiple platforms including Macintoshes and UNIX-based computers.

Just because you use Visual J++ and program in the Java language does not limit you to ActiveX controls written in Java. No matter which language the developer used to create an ActiveX control, you'll be able to utilize that control inside your Visual J++ applet.

 
In a way, ActiveX provides executable content for your Web pages just as your Visual J++ applets do. The ActiveX controls, however, are more compact and work as individual units that you can combine with other ActiveX controls and access all of them inside a single applet. in addition, you'll be able to use those same ActiveX controls inside the desktop applications you write in Java and other programming languages.
 

ActiveX Plug-Ins

The ActiveX controls are virtually perfect implementations of objects that you can embed and use inside your Visual J++ programs. You don't have to have created the ActiveX control to use the control because the ActiveX's COM interface gives you the interface you need to access, update, and manipulate the control. Once you obtain an ActiveX control (or use Visual J++ to write your own), you can use that control in any and all Visual J++ programs that you write. The control works like Visual J++'s pre-defined classes in that the ActiveX controls save you programming time.
 
The ActiveX controls work like plug-in objects. When you want to include a Web page audio player to play a welcoming song for your end-users, you can find a pre-written ActiveX audio control and embed that control from your Visual J++ program. Using the control's COM interface, you'll assign the song (stored in an audio file such as a .Wav file) and trigger the song's play when the end-user is ready to hear the song.
 
Microsoft makes the believable claim that thousands of ActiveX controls are now available from Microsoft's own Web site, other Web sites, bulletin board systems, and software utility retailers. The Web sites listed in appendix B, "Visual J++ Sites on the Web," contain several ActiveX controls that you can access. Your interactive Web page development could literally become putting together the ActiveX controls you need, using Visual J++ as the development tool and Java as the glue.

Your ActiveX-Based Web Browser

Before you or your end-users can utilize ActiveX controls, you must have an ActiveX-enabled Web browser. Microsoft's Internet Explorer 3.0 supports ActiveX controls, so if you or an end-user uses Internet Explorer 3.0, you'll have no problems. Netscape's Navigator pre-3.0 release versions do not support ActiveX controls, but Netscape has provided an ActiveX plug-in utility.

 
ON THE WEB
http://www.ncompasslabs.com/binaries/download_plugin.htm Go to this Web page to download the ActiveX plug-in utility for versions of Netscape Navigator prior to version 3.0.
 
 
Your Web browser provides a Java Virtual Machine as you learned in the first part of this book. The Java Virtual Machine executes the machine-independent Java bytecode. When your Web browser is ActiveX-enabled, the Java Virtual Machine includes an ActiveX interpreter that reads and controls all of your applet's ActiveX controls as well.

Figure 17.1 shows how your ActiveX control integrates into the Visual J++-Java-Web browsing environment.


FIG. 17.1

Visual J++ creates your Web site's Java-based applet and combines ActiveX controls with the applet.

Check Out ActiveX Samples

If you point your ActiveX-enabled Web browser to the following URL, you'll be able to view a rich collection of Microsoft-supplied ActiveX controls:

http://www.microsoft.com/activex/gallery/

Figure 17.2 shows the site's opening page. The lefthand window lists each ActiveX control by category. Scroll through the list to find a control you're interested in. Not only can you read about the controls, but you can even download the control and use the ActiveX control inside your own applet.

 
Although Microsoft welcomes your visitation to their Web site and encourages you to download and use the ActiveX controls, some of the controls are limited by copyright specifications. Many vendors gave Microsoft the license to provide their ActiveX controls on this Web page but they limited the time users can use those controls. Therefore, you'll find controls that work only once or for a limited amount of time. You'll be able to learn about ActiveX from these samples and many of the controls have no such time limitations. Microsoft lets you freely use some of these controls as explained on the Web page's documentation.


FIG. 17.2

You can download and try numerous ActiveX controls from Microsoft's ActiveX Web site.

Spend some time at this ActiveX site and study the ActiveX examples that you can view and download. Figure 17.3 gives you a better idea of what you can expect when you click a control's name. Figure 17.3 shows the information screen for the Stock Ticker ActiveX control (located under the Real Time Data Acquisition category at the left of the Web site). The screen describes the control that you can embed in your own applets to display data from an URL that continuously updates at a pre-selected time interval. When you click this control's Control Information Page hypertext link, Internet Explorer (or your ActiveX-enabled browser such as Netscape Navigator 3.0) automatically downloads the control to your computer and executes the control on the control's Web page as if Microsoft had sent the control straight to you as an embedded applet.

 
Some of the controls consume a lot of disk space so be sure you have plenty of extra room (at least 20M of free space if you want to view several ActiveX controls). In addition, many of the controls take several seconds and perhaps several minutes to arrive on your machine. If you use a 14.4Kbps or 28.8Kbps modem, be prepared to wait for any ActiveX page to load.


FIG. 17.3

Click an ActiveX control to see what the control does.

Where does the Stock Ticker ActiveX control get its information? In other words, how does the Stock Ticker ActiveX control know which URL to display? You have to tell the Stock Ticker ActiveX control which URL to show if you use the control inside your own applet. Before you can fully use a control, you must download the control's type library from the vendor to your own system.

 
Once the control downloads sample stock information from its pre-defined Web site of stock information, the two tickers scroll across your Web page quickly. Remember that you're viewing executable content that works just like your Visual J++ applets; the ActiveX control is using your own computer's resources to do its job and the scrolling is not hampered by download times.

A Brief Look at ActiveX Code

When you obtain an ActiveX control that you want to use in your applet, you will get documentation that describes the control's interface. Just as a class contains an interface that defines data members and methods (see chapter 19, "Visual J++ Applet and AWT Class Refernece," for examples of some pre-defined class interfaces), the ActiveX control also contains data members and methods you can access within your applet to make the control behave the way you want it to behave.

There is actually nothing special you need to do other than install the ActiveX control according to the ActiveX control's installation instructions, insert the appropriate import command inside all your Visual J++ code that will use the ActiveX control's type library (which is nothing more than an ActiveX control's class definition that conforms to the pre-defined industry-standard COM specification), and then use the ActiveX control as if it were an object.

For example, suppose you want to place a pop-up menu on your applet's window. Instead of using the AWT features, you obtain an ActiveX menu control that sports the following features:

Let's call the ActiveX menu control FancyMenu. The FancyMenu's documentation lists all the pre-defined named constants and methods you can use to display and manipulate the menu as well as how your applet will get the user's selected menu choice. After you install the FancyMenu control, you'll import the control's type library into your Visual J++ program by using the following line at the top of your class' code:

import FancyMenu.ActiveX.control; // Imports the COM object

You then instantiate a menu object just as you do any other object. According to FancyMenu's documentation, you might use the FancyMenu class (remember that a COM object looks just like a class to the Java language) to instantiate a menu object with the following line of code:

FancyMenu userMenu = new FancyMenu; // Instantiate the menu

Perhaps you must tell the ActiveX control how many menu choices your applet requires as well as specify the text for each choice. Although we're describing an imaginary ActiveX control, the following code is similar to what you might write for such a control:


userMenu.setChoices = 5; // Request a total of five choices
userMenu.SetChoiceText(1) = "Add items"; // 1st option
userMenu.SetChoiceText(2) = "Change items"; // 2nd option
userMenu.SetChoiceText(3) = "Delete items"; // 3rd option
userMenu.SetChoiceText(4) = "Print items"; // 4th option
userMenu.SetChoiceText(5) = "Close menu"; // 5th option

Perhaps a FancyMenu pre-defined variable holds the user's chosen selection. You might test the menu control's selection with a switch statement such as the following:


switch (userMenu.choice)
{
case 1: { addItem(); // Calls my addItem() method
break; }
case 2: { chaItem(); // Calls my chaItem() method
break; }
case 3: { delItem(); // Calls my delItem() method
break; }
case 4: { priItem(); // Calls my priItem() method
break; }
case 5:
default: { userMenu.hide = true; // Get rid of the menu
break; }
}

What's new about ActiveX programming? Nothing. That's the idea. Once you install an ActiveX control, your Visual J++ program will work with the control, controlling the control in effect, just as your code manipulates and manages objects that you define inside your class.

Summary

The goal of this chapter was to teach you about the ActiveX and COM technologies. As you saw, ActiveX brings a lot of new technology to your programs but does not bring a big programming burden to you. Programming ActiveX controls is just as easy as programming your own class's objects.

As long as your Web browser is ActiveX-aware, as is Internet Explorer 3.0, you will be able to view Web pages that contain such applets. Of course, your end-users must also have an ActiveX-aware Web browser but the ActiveX/COM technology is gaining ground so rapidly and so many companies are supporting the standard, that most Web browsers should be ActiveX-aware soon if they are not already.

Here are the points this chapter covered:


Previous Page TOC Next Page

| Previous Chapter | Next Chapter |

| Table of Contents | Book Home Page |

| Que Home Page | Digital Bookshelf | Disclaimer |


To order books from QUE, call us at 800-716-0044 or 317-361-5400.

For comments or technical support for our books and software, select Talk to Us.

© 1996, QUE Corporation, an imprint of Macmillan Publishing USA, a Simon and Schuster Company.