by Joe Weber
Visual J++ is Microsofts 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 excelerated 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 by clicking buttons or pressing single key-sequences you can edit, compile, run, and debug code. In addition, compile-time errors can be clicked to bring you directly to the line of offending code.
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.
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 Microsofts 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 Microsofts developer suite, the environment is designed so that you can learned 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 which 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 fig. 49.1).
Select the editor emulation which you are most comfortable with.
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 49.2.
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. There 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.
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.
Lets use J++ to create a simple HelloWorld applet:
Choose File, New.
J++ prompts you to tell it what kind of an item you want to create. You dont just want to create a text file like you might if you were using edit, and J++ deals with sets of files in what it calls project files. So, in the New dialog box, select Project Workspace as shown in figure 49.3.
You can open several new types of objects in the New dialog box.
The next dialog box that appears asks you to select the project name, and to choose from several wizards. In this case, you want to create a standard Java Workspace, as shown in figure 49.4.
Enter the new project name.
The next task is to create the actual HelloWorld class. To create the class:
Move your mouse pointer over the HelloWorld Classes and right-click, as shown in figure 49.5. This should open a dialog box as shown in figure 49.6.
Right-click the project icon and select Create New Class.
By specifying most of the standard information about a class, J++ autogenerates some code for you.
This dialog box asks you all the standard information that you might need to know about a class. The first item that needs to be filled in is the class name. The class name must be a valid class name, but fortunately, J++ warns you if you try to use a invalid identifier.
The next item is the extends window, which should contain the name of the class that your new
class will extend (if any). For the HelloWorld class, you are extending Applet, so fill in the E
xtends field with java.applet.Applet. Your HelloWord applet will not be part of a package so
you can leave that blank, but you do want the class to be public so make sure the public modifier is
checked.
Noticeably absent from the list of items that J++ prompts you for are classes to implement. Fortunately, you can change all of the information which you enter into this dialog box later. Doing so simply requires writing the source code yourself. In the same vein, you need not worry if you need to later extend a class when originally you didnt anticipate the need.
When you click OK, Visual J++ generates a template for the class that you just specified. Notice in figure 49.7 that Visual J++ did several other things. First, you can now see the HelloWorld class under the ClassView pane. Second, Visual J++ automatically imported the java.applet.Applet class and choose to extend Applet, as you specified in the New dialog box.
Visual J++ automatically generates a lot of the code for you.
Now that you have the basic structure of the class, lets add in the paint method. To add a method:
Move your mouse over to the ClassView pane, and right-click the HelloWorld class. You should now have more options than you had with the package.
Select Add Method. This should bring up the Add Method dialog box as shown in figure 49.8.
Create the paint method.
As with adding the class, Visual J++ prompts you to specify each of the properties for your method. Visual J++ prompts you to first enter the return type. The paint method has a void return type, so fill that into the first dialog box.
J++ then asks you to enter the rest of the method declaration. While it might not be entirely clear that what J++ is asking for is the method name and parameters, fill them in here. The rest of the method modifiers should be specified in the checklist.
When you click OK, J++ creates the method prototype for you. However, you should notice immediately that it does not actually import that java.awt.Graphics class for you. To solve this, you must go to the top of the source code and manually add the import statement.
You are now free to fill out the body of the paint method as you learned in Chapter 15: Writing an Applet.
(d)Reading in Code that is Already Written
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:
If you just open the source code (by choosing File, Open), the file does not get
added to the ClassView pane, nor is it added to the project files).However, when you add a class
file to a project, J++ automatically reads it through and extracts all method and variable
information, so that it appears on the ClassView pane.
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, 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.
[fig49-10, fig49-11, fig4912]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+F8. This causes Microsofts accelerated Java compiler to compile the active class.
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 our HelloWorld application, this wont 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 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, Create a 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 Graphic (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 49.9. These sections are the Edit window, toolbar, color palette, and preview screen.
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 its 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 to 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.
Lets create a simple dialog box to make people acknowledge our 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, R
esource. This opens a list of several possible resources to add. As shown in figure 49.10, 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.
Add a dialog resource to bring up the Resource Editor.
Once you have selected the dialog, click OK. This should bring up the Dialog Editor. This nifty utility allows you to lay out any GUI component graphically on-the-fly. As you can see in figure 49.11, the main two portions of the editor are made up of the design space and the component controls list. By selecting tools from the component controls, you can paint them onto the design space.
By using the component controls, you can design any GUI window in the design space.
For the License dialog box, use the tools in the Component Controls window to fill out the design space, as shown in figure 49.13.
Now that you have the window laid out the way you want it, lets make one more change. Do you see the label on the window? Wouldnt it be nice to make it more descriptive?
You can change the properties of any element on-screen by first selecting it with your mouse, and
then pressing Alt+Enter. 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 49.12. Change the C
aption 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.
Press Alt+Enter to edit the properties of any element on the screen.
While your 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, S
ave. There is no OK button on the screen; just save it as you would any other 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 LICENSE.RCT file you just created, and click Finish.
The Resource Wizard automatically create 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 Insert, Files
Into Project.
Now that you have the code available to you, lets go in and make a few modifications to the 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 49.1, you need to check the parent class for its Frame.
Listing 49.1 A new constructor for license and the getFrame() method
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 still 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 thingyouve 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 thats 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 dont 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 doesnt 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 49.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.
Listing 49.2 Handle buttons using the handleEvent method.
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 dont want the window to stay on-screen after the user has already approved the agreement.
| 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