This article may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. To maintain the flow of the article, we've left these URLs in the text, but disabled the links.


MIND


This article assumes you're familiar with Visual J++ 1.x.
Download the code (1KB)

Visual J++ 6.0 Sneak Preview
Jonathan Locke

It’s a development environment! It’s a new way of tackling Java development! It’s a dessert topping! Visual J++ 6.0 lives up to all your wildest expectations. In fact, it’s 5.0 better than version 1.0.
If you have used Microsoft® Visual J++™ 1.0 or 1.1 in the past, you are going to find Visual J++ 6.0 to be a major step forward. It is immediately obvious that Visual J++ 6.0 was rethought and redesigned from the ground up. And, not unexpectedly, the resulting product is a great deal more usable and has a plethora of new features.

What's New?
      Probably the biggest overall shift in Visual J++ 6.0 is the orientation of the development environment, which now more accurately reflects the nature of enterprise software development. Visual J++ now includes robust features that assist in developing client-server solutions, encourage more effective reuse of ActiveX® and COM components, make database access easier, and even help to package and deploy finished solutions.
      The other big addition in this version is the debut of the Windows® Foundation Classes (WFC) for Java. WFC is a relatively thin wrapper that effectively serves two purposes: it wraps the core Win32® API functionality in an object model, and it embraces Dynamic HTML (DHTML). This is wonderful news if you're ready to give up on the all-but-broken promises of Java's AWT (write once, debug everywhere). And from what I have seen so far, it appears that WFC will be a far more effective way to write Java interfaces on Windows-based machines. Unfortunately, for those of you who are still in the cross-platform compatibility camp, Visual J++ 6.0 has little new to help you beyond Microsoft's blazing compiler and helpful coding environment (which might still make it worthwhile for you).
      Thanks to a Windows-centric WFC, it is no longer necessary to learn layout managers, which were a least-common-denominator solution designed for cross-platform compatibility. One of the first things you will notice about Visual J++ is that it now has a complete drag-and-drop GUI page designer. The WFC Designer seamlessly supports access to ActiveX and COM controls and also allows you to create your own reusable WFC controls. The Designer is really well done, and makes it especially easy to create WFC user interfaces. Let's take a look at some of the steps involved in creating an application with a simple UI.

Creating a Project
      You create a project in Visual J++ 6.0 by selecting New Project from the File menu (see Figure 1). Here, I am creating a Windows-based application called VJ6WizTest by using the Application Wizard. The first dialog in the wizard contains a profile dropdown (see Figure 2). This will enable you to streamline the process of using the wizard by saving and later reusing your typical wizard settings.
      You can choose between creating a Form Based Application and a Form Based Application with Data. I decided to create the former (see Figure 3). In Figure 4, I added a menu and an edit control to create a Notepad-like application. (At the time of this writing, toolbars and status bars were unimplemented.) Figure 5 shows that I included all possible comments. Finally, Figure 6 illustrates the choice of packaging and deployment options; I chose to create an EXE file. Before actually creating the project, a summary page for the wizard appears (see Figure 7). Here you can review the decisions you made and save those choices to a profile if you want to make the same choices in the future.

Welcome to the Visual J++ Environment
      Once the project has been created, your development environment will look something like Figure 8. The Project Explorer in the upper-right corner shows the new file JPad.java that was created by the wizard. The center of the screen is the WFC Designer and shows the JPad application, ready for editing. The left side of the screen shows a toolbox of WFC controls that can be dragged into the Designer for use in the application. The lower-right side shows an editable list of properties for the form (or whatever form component is currently selected). Finally, the lower-middle portion of the screen shows a list of tasks to be performed. This list shows TODO comments created earlier by the wizard. New tasks are placed in this list until they are completed.
      Right-clicking on the JPad form and selecting View Code displays the code for the form in the Designer area. You can switch back to the Designer window by simply selecting the appropriate MDI window from the Window menu or by right-clicking on the JPad.java file in the Project Explorer and choosing View Designer. Scrolling around in the code created by the wizard, you will discover an area of the code that is displayed with a dark gray background. This coloring indicates that the code is maintained by the Designer and that you should not modify it. Attempting to modify Designer-created code will produce a dialog stating that the code cannot be modified. Although you can modify the code outside of the Visual J++ WFC Designer, it is not recommended.
      The Designer-created code includes a set of initialized package variables, one for each component in the form. Since the New Project wizard created a number of menus and an edit control, this is what you see in the code. Following the variables is a private method called initForm, which is used to initialize the state of all the components in the form and to hook up component event handlers. The handlers for the various events hooked up by the New Project wizard appear throughout the JPad.java file.
      The Project Properties dialog is invoked from the Project menu. It contains various project options, including how you want to launch the program, compilation options (now including #define symbols, since the Visual J++ 6.0 compiler supports conditional compilation), custom build steps, classpath management, COM options (to make Java classes into typelibed COM classes), and packaging and deployment options. Like its big brother, Visual C++®, each of these options can be set for different build configurations (debug and release, for example).

Adding an AboutBox Dialog
      As an example, I'll add my own event handler for the Help About menu. To do this, I switch to the Designer, pull down the Help menu, and click on About. This adds the following event handler to JPad.java:


 private void aboutMenu_click(Object sender, Event e)
 {
 
 }
The Designer-maintained code is then changed to include a line that hooks up the event handler:
 aboutMenu.addOnClick(new EventHandler(this.aboutMenu_click));
      I add a new item to the VJ6WizTest project by right-clicking on VJ6WizTest in the Project Explorer, selecting Add, then selecting Add Form. The resulting dialog is shown in Figure 9. I then add a form called AboutBox to my project. The AboutBox form is really just a stripped-down version of the JPad form. I drag a text label and a button from the WFC Controls toolbox to the form and change the titles of each control by using the property editor.
      Finally, I add the following lines to aboutMenu_click:

   private void aboutMenu_click(Object     
   sender, Event e)
   {
       AboutBox a = new AboutBox();
       a.showDialog();
   }
This creates an AboutBox form and displays it. Hooking up the OK button to close the AboutBox is accomplished by simply changing the cancelButton property for the form to button1 (the name of the OK button). Figure 10 shows the final result.
      Now, I'm sure some of you are wondering about the following statement:

   aboutMenu.addOnClick(new                  
   EventHandler(this.aboutMenu_click));
What does this mean? Well, EventHandler is a new language feature specific to Visual J++. It's a type-safe method pointer created using a new language feature called a delegate. A delegate is declared at the class declaration scope like this:

   delegate long intOp(int a, int b);
When compiled, this creates a class called intOp that extends com.ms.lang.Delegate. This class can then be instantiated with the new operator, passing in the name of a target method to invoke as an argument to the constructor. Figure 11 demonstrates how to work with delegates. The newly constructed delegate object (called op in the code shown in Figure 11) can then be used to invoke the method to which it points. This is done by calling the delegate's invoke method, passing arguments of the same types originally declared in the delegate statement.
      The beauty of delegates is that it no longer matters what object or method is being called by a delegate object, yet the type safety of the Java language is still preserved. Delegates obviate essentially all of the ugliness and inefficiency of the current AWT listener/adapter scheme. It's no longer necessary to create nested classes just to hook up listeners, and your listener methods can have any name you like. In addition to simple delegates, you can declare multicast delegates that invoke a list of methods in a particular order. Since it is not clear which method invocation's return value should be returned from the invoke method, all multicast delegates are declared as returning void.

IntelliSense
      In working with the DelegateTest example and the code generated by the New Project wizard, I was very happy to find IntelliSense® technology in Visual J++. While you type, Visual J++ is constantly in the process of evaluating your code. When compiler errors occur, you know about them instantly because the Task List is updated to include the errors, and squiggly red lines underscore your mistakes on the screen (just like they do in Microsoft Word). When classes and methods are declared, they immediately become part of the environment's knowledge of your code. This knowledge is used to help you complete code statements.
      For instance, when I typed the statement in the DelegateTest example that reads op.invoke(10, 5), typing just "op." caused a dropdown to appear, containing the various methods that I might choose to complete the statement. I typed "in" and hit the spacebar. IntelliSense then completed the word with "voke." I then typed a left parenthesis and IntelliSense provided a tooltip showing the method's prototype as long intOp.invoke(int a, int b). The wonderful thing about this kind of assisted coding is that it's really easy to get used to. The downside is that it can be kind of addictive.

Debugging
      The Visual J++ debugger has come a long way. This latest version is very usable and has addressed all my criticisms of Visual J++ 1.0. One particular gripe that I'm happy to say has been resolved is the debugger's handling of exceptions. In Visual J++ 6.0, the debugger lets you use the exception class hierarchy when setting exception handling options. For each exception in the hierarchy, you can choose "Break into the debugger," "Continue and break only if the exception is not handled," or "Use superclass setting." The UI is neat, intuitive, and gets the job done quite nicely.
      Most other features of the debugger are similar to the previous version, if not identical. You can expect the usual auto, local, and watch windows in addition to tooltips for variables and a call stack dropdown. In general, debugging with Visual J++ is now much closer to what developers have come to expect from Visual C++. In fact, Visual J++ now even supports interprocess (and intermachine) debugging as well as integrated script debugging. Unfortunately, integrated native C/C++ debugging isn't supported yet. I imagine this would be quite a trick, though, and I'm happy enough to wait for support in some future version.

Other Extras
      Visual J++ 6.0 has support for ActiveX Data Objects 2.0, which means scalable, high-performance data access to any data source on any platform. Access to data services can now be achieved through HTTP. To assist in the design stages of a database project, Visual J++ 6.0 includes a SQL Query Designer and a full-featured Database Designer. The Query Designer is useful in constructing database queries, while the Database Designer can be used to create and modify database tables and schemas visually.
      The Visual J++ class browser appears in the Class Outline pane, replacing the Designer's toolbox. The class browser is even better than the last version (which I thought was fairly nice in its own right), and includes lists of superclasses and inherited members. In addition, right-clicking on class browser items unveils some useful options, including the ability to go to the definition of a given item, override a method, add a method, create a class, add member variables, and invoke a class builder on the class. The class builder is an interface for modifying classes.
      In addition to the basic coding tools, Visual J++ has a few nice bells and whistles that deserve mention. An integrated WYSIWYG HTML editor is included. The editor supports script authoring features, integrated source control management, and Microsoft Transaction Server 2.0. Project deployment options are available to help speed your project releases. There is a help system, but the prerelease version I reviewed was rather incomplete in terms of content. The ability to define window layouts and recall them at a later time (the environment comes with a variety of useful predefined UI configurations) is also a plus.

Conclusion
      All in all, Visual J++ 6.0 is miles ahead of the 1.0 version and looks to be a very useful and powerful development environment. Visual J++ 6.0 doesn't provide much new assistance for cross-platform development (although it still supports it), but if you're primarily targeting the Windows platform you will find this package—and WFC in particular—a much improved development tool.

From the July 1998 issue of Microsoft Interactive Developer.