by Joe Weber
Visual J++ is Microsoft's entry to the Java Tools. Visual J++ is a complete Integrated Development Environment (IDE) based on its Developer Studio products. In addition, J++ includes a visual design tool, an accelerated bytecode compiler, its own Appletviewer, and maintains a great deal of extensibility.
Microsoft came late to the Java developers tools market, but it did so with a splash. By using Visual J++, you can greatly reduce your programming time, as well as increase the speed at which your resulting code runs.
Unlike the traditional Sun JDK development system, Visual J++ is a complete development environment. The difference between the two is fairly profound.
The most profound nature by which an IDE changes programming is by providing a single program from which you can edit, compile, run, and debug code by clicking buttons or pressing single key-sequences. In addition, compile-time errors can be clicked to bring you directly to the line of offending code.
Visual J++ offers context-sensitive syntax highlighting. This means that keywords
are placed in different colors so you can quickly see the parts of the code that
most interest you. In addition, you can edit two sets of code side-by-side as shown
in Figure 55.1.
FIG. 55.1
You can edit two sets of code side-by-side.
Visual J++ provides a built-in automatic code-generation tool for templates of standard programs such as applets. It also provides a visual development tool that greatly reduces the amount of time it takes to lay out GUI elements on the screen.
Stretching a bit beyond the programming aspects, Visual J++ also provides a built-in graphics editor, recognizing the tight interaction graphic design has with elegant programming.
NOTE: Microsoft uses its standard installation system for installing Visual J++. You will want to follow all the instructions provided with the Visual J++ product.
Programmers familiar with Microsoft's Visual C++, FORTRAN Power Station, or Visual Test will find that Visual J++ provides a familiar and comfortable interface. For those not so familiar with Microsoft's developer suite, the environment is designed so that you can learn quickly.
At the heart of any development system is the ability to edit source code. Like most modern editors since Brief, Visual J++ offers context-sensitive highlighting to help quickly identify sections of code. One interesting addition that Microsoft includes is separating JavaDoc comments with standard comments. JavaDoc comments appear in gray, while normal comments are light green.
One unique feature of the Microsoft editor is the ability to emulate a large number
of other editors. Users familiar with BRIEF or Epsilon can force the editor to emulate
the key strokes, text selection, and window display of these two editors. In addition,
you have the option to mix and match the controls of each using the Recommended Options
checklist. By using the checklist, you can create a custom emulation (see Figure
55.2).
FIG. 55.2
Select the editor emulation with which you are most comfortable.
To change the emulation, open the Options dialog box by choosing Tools, Options. The fourth tab is labeled Compatibility. You should see several options that you can mix and match, but the easiest solution is to use the default options.
Included in the J++ Help is a completely rewritten version of the Java API. The
API is structured exactly like Microsoft has structured the rest of the help and
class browser features, as shown in Figure 55.3.
FIG. 55.3
The API is included in the online help.
In order to use the Help, you must first select the InfoView tab. Here you want to find the Java API section. From there find the Java API Reference, inside of which are two volumes: The first is core classes that contain io, lang, util, and net. The second volume contains help on awt, awt.image, awt.peer, and applet.
You may have noticed in Figure 55.3 that the Help looks very much like HTML, and
that the Help window actually has some buttons that look like a browser. Well, that's
not coincidence, J++ uses an integrated version of Internet Explorer to view the
API and, if you would like, you can also jump to any other Web page on the Net as
shown in Figure 55.4.
FIG. 55.4
Using the integrated browser, you can jump to any page on the Net.
NOTE: The use of Internet Explorer to view Help is a new feature of Visual J++ 1.1 and is not available in the 1.0 version.
One unique aspect of how Visual J++ works is its use of projects. Projects are used to keep track of all the files that need to be compiled when you want to compile a Java applet and to store information about classes.
Use J++ to create a simple HelloWorld applet:
FIG. 55.5
You can open several new types of objects in the New dialog box.
NOTE: Under J++ 1.0 you need to go through two dialog boxes to accomplish this task. The next dialog box that appears asks you to select the project name, and to choose from several wizards.
NOTE: You may also want to use the Java Applet Wizard. This wizard walks you through several easy steps, and automatically generates much of the code that is required for many standard applets.
The next task is to create the actual HelloWorld class. To create the class:
FIG. 55.6
Right-click the project icon and select New Class.
FIG. 55.7
By specifying most of the standard information about a class, J++ autogenerates some
code for you.
FIG. 55.8
Visual J++ automatically generates the code for you.
Now that you have the basic structure of the class, add in the paint method. To add a method:
FIG. 55.9
Create the paint method.
You are now free to fill out the body of the paint method as you learned in Chapter 14, "Writing an Applet."
NOTE: While this chapter only talks about adding methods and variables as they are automatically generated by Visual J++, you are by no means limited to doing it this way. If you would prefer to type in all the code yourself, Visual J++ takes care of adding the appropriate items to the ClassView pane.
When you first pick up Visual J++, you will probably want to work with source code that you have already written. You can bring this code into the editor in two ways:
NOTE: Under J++ 1.0 to add existing code to a project choose Insert, Files Into Project.
One of the most useful features associated with Visual J++ is the ClassView pane. From the ClassView Pane you can see all of your classes and, by diving deeper into them, you can see all the methods and variables of your classes.
Using the HelloWorld class that you just created, double-click it so it shows the paint() method, and then try double-clicking the paint() item in the ClassView pane. You should see your cursor go directly to this method. When projects grow large, you will find that this feature alone is worth the price of admission.
Source code is only half the battle. In order for source to do you any good, as you know, you must compile it. To compile just a particular class file, you can either click the Compile icon, or you can press Ctrl+F7. This causes Microsoft's accelerated Java compiler to compile the active class. You should notice right away that Microsoft's compiler is many times faster than javac. In fact, J++ can compile one million lines of code a minute!
Another advantage to using the integrated development tool now becomes obvious to you if you have any errors. The compiler produces errors very similar to those from javac. However, you can now double-click any error and Visual J++ automatically marks the location where the error occurred and moves your cursor to that location.
The second icon in the compiler list allows you to build an entire project. In the case of your HelloWorld application, this won't do anything different than the compile icon, but if you were editing a project with more than one class, Visual J++ would first look to see which files had been updated since the last compilation, and then would compile those files.
Microsoft has included a fairly rudimentary Graphics Editor with its Visual J++ tools. The Graphics Editor provides a set of tools for creating bitmaps to use as icons, cursors, or to support other portions of your programs.
To create a new graphic, choose File, New, Bitmap File. This should bring you into the built-in Graphics Editor. The editor currently supports the Bitmap (BMP) as well as the Graphics Interface Format (GIF) and the Joint Photographic Experts Group ( JPEG) formats.
The Graphics Editor is likely to remind you a lot of other paint tools you have
used. The window is made up of four basic sections, as shown in Figure 55.10. These
sections are the Edit window, toolbar, color palette, and preview screen.
FIG. 55.10
The Graphics Editor included with J++ can be used to create icons or other graphics.
Probably the most time-consuming part of every project is developing the GUI. Java has a number of great tools for laying out work, but it still takes time to do so manually. Fortunately, Visual J++ has a great resource to help you automatically create the source code for the layout.
The first step when using the Resource Wizard is to create a resource template. The resource template is where you actually lay out how you want the GUI to look.
Create a simple dialog box to make people acknowledge your licensing agreement. In this box, you want to have a text box in which you have the actual text for the license, an OK button, and a Cancel button.
To begin, you first need to create a new resource. To do this, choose Insert,
Resource. This opens a list of several possible resources to add. As shown
in Figure 55.11, the resource that you want to add at this point is a dialog. Actually,
Dialog is the option you will choose regardless of what kind of window you want to
use.
FIG. 55.11
Add a dialog resource to bring up the Resource Editor.
NOTE: At this point, you are bringing up a Dialog Editor. However, the way that the Microsoft J++ Resource Wizard works is not actually based around java.awt.Dialog, but a generic container. This means that you can use the Dialog Editor to not only edit dialogs, but windows, applets, or any other GUI part.
For the License dialog box, use the tools in the Component Controls window to
fill out the design space, as shown in Figure 55.12.
FIG. 55.12
By using the component controls, you can design any GUI window in the design space.
Now that you have the window laid out the way you want it, make one more change. Do you see the label on the window? Wouldn't it be nice to make it more descriptive?
You can change the properties of any element on-screen by first selecting the
element with your mouse and then pressing Alt+Enter (or double-clicking). To change
the label on the dialog box, select the entire box and press Alt+Enter. This should
bring up the properties window as shown in Figure 55.13. Change the Caption
to read License Agreement. As you type the new text in, you should
start to see it appear in the design space. You can also get to the Properties window
by clicking the window with your right mouse button and selecting Properties.
FIG. 55.13
Press Alt+Enter to edit the properties of any element on the screen.
While you're in the properties section, go ahead and change the ID of the dialog to license. The ID indicates the name by which a component will be known, and, in the case of the dialog window, that means the class name. Finally, to save the new resource template, choose File, Save. There is no OK button on the screen; just save it as you would any other file.
CAUTION:
Do not attempt to change the extension of the file to JAVA or anything else. RCT is the correct extension for the resource. Continue to the next section to produce the JAVA file.
When you save the dialog box, it is saved as a LICENSE.RCT. The RCT files are not actually usable in a Java program yet, so you must go through one additional step.
Choose Tools, _Java Resource Wizard. This should bring up another dialog box. Specify the location and the LICENSE.RCT file you just created, and click Finish.
The Resource Wizard automatically creates two Java files for you:
The Java class created when you run the Resource Wizard has two methods. First, it has a constructor that takes a Container object as an argument. The second method is CreateControls.
The second Java file creates a DialogLayout class. This class implements the AWTLayoutManager interface which allows the resources in your classes to be handled the same way the Windows resource files are handled.
The first step to using code generated using the Resource Wizard is to add the files to your project. To do this, add the source code exactly as you did before. Choose Project, Add to Project, Files.
Now that you have the code available to you, go in and make a few modifications to the license.class code. First, because this will be a Dialog, change the class declaration to make it extend java.awt.Dialog:
public class license extends java.awt.Dialog
Now, you need to make a few changes to the constructor method for the class. First, you need to include a super constructor call, because there is no Dialog constructor that matches the constructor you have for license. All of the Dialog constructors require a Frame. So where do you get that? As you can see in Listing 55.1, you need to check the parent class for its Frame.
Container parent; // Constructor //-------------------------------------------------------------------------- public license (Container parent ) { super(getFrame (parent),"License Agreement"); super(); m_Parent = this; this.parent = parent; CreateControls(); } public Frame getFrame(Container c) { if (c instanceof Frame || c == null) return((Frame)c); else return(getFrame(c.getParent())); }
Next, now that the license is itself the Dialog, change the m_Parent variable to point to this. The m_Parent is where the class is going to start adding the various components. There are still some reasons why you may need access to the parent Applet, so add a parent field to the class and point it at the incoming container. Finally, add a call to the CreateContols() method that Visual J++ created.
Now, most of the changes you need to make are already done, except for one. In the CreateControls() method, there is a line that reads:
Font OldFnt = m_Parent.getFont();
This line is fine, except for one thing--you've just called CreateControls at a point where the getFont() may return null. Unfortunately, this leads to all kinds of runtime problems that can be very difficult to track down. There are two alternatives to solving this problem. The easiest solution is to use the parent reference you set up before. If you change the line to read:
Font OldFnt = parent.getFont();
your program will run fine, and will not crash. Unfortunately, different programs handle fonts differently, and Internet Explorer is likely to behave differently than Netscape, and that's going to be different depending on the platform. To make things look basically correct, the solution is to specify a font that you know will work fine. You can do this by changing the line to read:
Font OldFnt = new Font ("TimesRoman",Font.PLAIN,8);
The next step is to import the new classes into your applet class. Switch to the class file containing the Applet and add the lines:
import licenseNewDialog; import NewMenu;
You don't actually need to include the DialogLayout class, because license already takes care of this detail. Next, in the init() method of the applet, add the New dialog box to the container with the lines:
licenseNewDialog dlg = new licenseNewDialog( this ); dlg.CreateControls();
The first line declares an instance of the license class, passing the this pointer to the constructor and specifying the applet as the container.
If you go ahead and compile the application everything should work--up to a point. The License dialog box appears, and it looks fine, but it doesn't do anything. How do you go about handling the OK and Cancel buttons? You need to add a handleEvent or action method to the license class.
Listing 55.2 shows how you can handle the buttons using the handleEvent method. You may want to do something other than dismiss the window (such as stopping the program if the user cancels your window), but for now just print something to System.out.
public boolean handleEvent (Event evt){ if (evt.id != 1001) return super.handleEvent (evt); if (evt.target == IDOK){ System.out.println("The License Agreement has been oked"); dispose(); } else if (evt.target == IDCANCEL){ System.out.println("The License Agreement has been denied"); dispose(); return true; }
Notice that in the handleEvent, the dialog box is disposed. This is a fairly important step. After all, you don't want the window to stay on-screen after the user has already approved the agreement.