Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Sams Teach Yourself Visual J++ 6 in 21 Days
(Publisher: Macmillan Computer Publishing)
Author(s): Rick Leinecker
ISBN: 0672313510
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Creating Your First Application

It’s time to create an application using Visual J++. Your application will be in the form of a compiled .EXE. Whereas the applet we created was loaded into Internet Explorer and then executed, the compiled application doesn’t require Internet Explorer.

Compiled Visual J++ applications are normally very small. This is because they don’t contain the runtime code that’s necessary for the application to run. So how does the application actually run when you execute the .EXE file? The compiled Visual J++ application almost immediately executes a program named WEXESTUB.EXE. This is part of the redistributable Visual J++ files. This program in turn invokes the Java Virtual Machine in such a way that it executes the byte code of the compiled application.


Note:  The Java Virtual Machine is the software that resides on Java-aware computer systems that interprets Java byte code. It’s something you won’t have to worry about as a Visual J++ programmer. Just write your programs and assume that Java-aware computers all have a functioning Virtual Machine.

You might ask yourself why Visual J++ would go through all that trouble. It might seem as though spawning an extra .EXE and then invoking the Java Virtual Machine is a lot of trouble. And you run the risk of encountering problems with both. What if WEXESTUB.EXE is missing or damaged, or what if the Java Virtual Machine is somehow missing?

All of these are good questions. But the fact is, Java has always relied on another application to correctly execute. Browsers such as Netscape and Internet Explorer are how users have executed Java for years. And the Java Virtual Machine has always been a required element of that. So it’s not too unusual that a similar system is part of executing Visual J++ applications.

These small applications take up less space—probably about a tenth the size of what a self-contained application would be. That size reduction is not trivial when you’re talking about installing to someone’s hard drive. Installation files will be smaller, and this might be very important if the installation files are downloaded from the Web.

Another good reason to rely on the Java Virtual Machine is that as it’s updated, the applications run better and faster. The applications don’t change, just the Java Virtual Machine does. So application developers benefit when users upgrade to a newer version of the Java Virtual Machine without having to deploy another version of their application.


Tip:  If a small number of users are experiencing unexplained behavior when they encounter one of your applets on the Web, suggest that they upgrade to a newer version of their browser. The newer version will invariably contain a newer version of the browser’s Virtual Machine. In most cases, this step will fix the problem.

Now to create the application. If you’ve just started Visual J++, the New Projects dialog box will open. If Visual J++ is already running, select File, New Project and the New Projects dialog will appear.

This time, we’ll select Applications in the Visual J++ Projects folder. As you can see in Figure 1.6, there are three application choices: Windows Application, Console Application, and Application Wizard. We’ll choose Windows Application.


Figure 1.6  For this project, you’ll select Windows Application from the Applications category.

Name your project Hello2. As you can see in Figure 1.6, the directory in which I’m creating the project is VJProjects\Hello2. Click the OK button and the project will be created.

After the project is created and you double-click on Form1.java in the Project Explorer window, you’ll see the default form in the center of the screen, as shown in Figure 1.7. This is a canvas on which you can add items from the toolbox by dragging and dropping.


Figure 1.7  The default form appears after your project has been created.

Let’s start off by adding a label to the form. In the toolbox, select Label. Move the mouse inside of the form, and you’ll notice that the mouse cursor changes shape, indicating that you’re ready to put a label on the form. Point the mouse to the location at which you’d like the upper-left corner of the label to appear, and hold down the left mouse button. While holding down the left mouse button, draw the mouse down and to the right until the label is the desired size. You can always change the label’s size and position with the mouse.

After the label is placed on the form as shown in Figure 1.8, you’ll notice that the Properties window changes to the properties of a label. The list contains quite a few properties, all of which change the appearance of the label. The one we’re going to change now is the text property. Click in the text property field and change the text to Hello Visual J++ World. As you type, the label in the form will change.


Figure 1.8  Labels are easy to add to Visual J++ application forms.

Compile the application by selecting Build, Build. Then select Debug, Run. Your application will appear, displaying your Hello Visual J++ World message.

By now you’re probably wondering where the source code is for the application. Visual J++ makes every effort to keep things visual so that developing applications is easy and intuitive. That’s why you see the form and not the source code by default. But to see the source code in the editor is easy. Just double-click on the form (not the form that’s executing as an application, but the form that’s part of the Visual J++ editor).

Scroll down through the source code. You’ll see the following lines as a result of adding the label from the toolbox:

label1.setLocation(new Point(20, 30));
label1.setSize(new Point(210, 40));
label1.setText("Hello Visual J++ World");
label1.setTabIndex(1);
this.setNewControls(new Control[] {
    label1});

Let’s experiment. Go back to the form by double-clicking on Form1.java in the Project Explorer window. Then, click on your label so that its properties are shown in the Properties window. In the font property field, click on the button with the “…” label, and a font selector dialog will appear. Now, change the font size to 14 and click the OK button. Finally, edit the location property to a value of 10, 50. Build the program and then go to the source code. The changes will be reflected in the source code, even though you didn’t touch the source code. Your source code should look as shown next.


Note:  The first line of code in the following code block, shown in bold, is what was added by Visual J++. It simply sets the font for the label.
label1.setFont(new Font("MS Sans Serif", -19));
label1.setLocation(new Point(10, 50));
label1.setSize(new Point(210, 40));
label1.setText("Hello Visual J++ World");
label1.setTabIndex(1);
this.setNewControls(new Control[] {
    label1});

Your application’s label displays text that’s larger than before. Now let’s add another label below the existing one with text that says Visual J++ is Cool. When you rebuild your application, it will look something like the one in Figure 1.9.


Figure 1.9  Your application should look something like this one.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.