HelloWorld1 Project

It seems almost obligatory that the first app in any coding tutorial be the famous "Hello, world" program, so let's begin there.

NOTE
For those of you who are familiar with Java, the "Hello, world" app will already be well known. This app is still a useful exercise for you because we will use it to explore many features of the Visual J Plus Plus v6 environment.

What's a Console app?

The simplest type of app is the console app. Console apps have no windows to deal with; they are designed to be run from the DOS command line. This might be the command line of an MS-DOS window in Windows or of DOS by itself. The output of a console app is simply spit directly back to the same command line.

You can run a console app under Windows by double-clicking the file. This brings up an MS-DOS window within which the console app runs.

TIP
Everything I've said here about MS-DOS windows running in Microsoft Windows applies equally well to the UNIX prompt running under Motif.

Although console apps are not very exciting, they're the preferred type of app to use when you're concentrating on performing a function—like calculating pi to the nth digit—and you don't particularly care about attractive output. This type of app is useful for many of the simple utilities you write for your own benefit.

Creating a Console app Project

In the Microsoft Development Environment (MDE), choose New Project from the File menu. This opens the New Project dialog box. A project is a collection of source files, which together build a program.

The New Project dialog box lists two options: Visual J Plus Plus Projects and Visual Studio. In the Visual J Plus Plus Projects folder, choose the apps folder and then choose Console app. Change the project name from Project1 to HelloWorld1. The HelloWorld1 project will be stored in a subfolder with the same name as the project.

The default is for Visual J Plus Plus to create the HelloWorld1 folder as a subfolder of \\MyDocuments\VisualStudio. Personally, I think this is a crazy place to put app folders. I create a separate Java folder to store all my apps and applets. (I also have one for Microsoft Visual C++ and one for Visual Basic.) Do as you like, but for this first project I chose Browse in the dialog box and hung my new project subdirectory off the folder c:\user\Visual J Plus Plus Programs\Generic apps. My results are shown in Figure 1-1.

Once you've renamed the project and placed it in the folder of your choice, choose Open. (You can also double-click Console app.) This causes Visual J Plus Plus to build the HelloWorld1 project.

Java Click to view at full size.

Screenshot-1. The New Project dialog box showing the app choices, project name, and project folder.

Project Explorer

To look at the results of our work, open Project Explorer. Project Explorer is to projects what Windows Explorer is to files and Microsoft Internet Explorer is to the Internet: a view into the contents of the project.

The most straightforward way to open Project Explorer is to choose Project Explorer from the View menu. There might also be a button on the toolbar that does the same thing. (I say might because you can customize the toolbars to include any buttons that you want. We'll talk about customizing toolbars later in this chapter.)

You can view the files in your HelloWorld1 project by clicking the plus sign next to the project name (just like viewing folders in Windows Explorer). The results are shown in Figure 1-2.

Screenshot

Screenshot-2. The Project Explorer view of our first project.

Project Explorer in Visual J Plus Plus v6 looks similar to the project view in version 1.1. The major difference is that Project Explorer refers to something called the HelloWorld1 solution and lists it as having one project.

What's a solution (other than something mixed in a liquid)?

Visual J Plus Plus v6 organizes projects into groups called solutions.

NOTE
A solution is a group of projects. Visual C++ 6 and Visual Basic 6 support almost the same functionality but call it a workspace.

Grouping projects in this way enables you to perform the same operation on all of the projects at once with a single command. For example, choosing Build on the Build menu rebuilds all projects in the solution.

Even though they are grouped together, projects retain their identity within the solution. For example, projects are deployed independently. Thus, if your solution has a Project1 and a Project2, rebuilding the solution might generate two executable files, Project1.exe and Project2.exe. Projects also have their own properties, so one project might generate an app while another generates an applet.

For now, we will limit ourselves to single-project solutions.

HelloWorld1 Project Source Code

Within the HelloWorld1 project is a single Java file, Class1.java. Double-click this file in Project Explorer to view the source code file the Console app Wizard created for us when we opened the new project.

NOTE
All examples for this chapter are located in the Generic apps subfolder on the tutorial's companion CD.

The following code shows the contents of Class1.java. I've added one line (shown in boldface) to convert the project into the "Hello, world" app.

/**
 * This class can take a variable number of parameters on the command
 * line. Program execution begins with the main() method. The class
 * constructor is not invoked unless an object of type 'Class1'
 * created in the main() method.
 */
public class Class1
{
 /**
 * The main entry point for the app. *
 * @param args Array of parameters passed to the app
 * via the command line.
 */
 public static void main (String[] args)
 {
 // TODO: Add initialization code here
 System.out.println("Hello, world");
 }
}


As you can see, the Console app Wizard constructs an empty class definition with comments. Notice that the comments are somewhat unusual: they begin with a /** rather than the normal /*. Also notice the appearance of * @param in the middle of the comments.

NOTE
The comments preceded by /** are generally called "Javadoc comments" after the Oracle utility Javadoc, which uses these comments to automatically generate class documentation. Visual J Plus Plus v6 has extended this concept into a powerful, dynamic feature described later in this chapter in the "Statement Completion" section.

Program execution begins with the public static method main().

NOTE
The public keyword makes the method visible to other classes, and the static keyword enables the method to be invoked without an object.

In the line of code I've added, System is a global object that retains information about the environment in which the program resides. One of the System object's data members is the static member out that points to standard output, normally the command line. The out data member is of class PrintStream. This class has a method println(), which prints whatever string is passed to it followed by a newline character.

NOTE
Under C++, the syntax for accessing out would have been something like System::out; however, Java does not use the :: symbol, preferring instead to stick with the single dot.

To compile HelloWorld1, choose Build from the Build menu. If the program compiles properly, the phrase "Solution update succeeded" appears in the status bar.

To run the program, you have four options:

  1. In the Visual J Plus Plus user interface, you can choose Start from the Debug menu.
  2. In Windows Explorer, you can double-click HelloWorld1.exe.
  3. In an MS-DOS window, you can go to the directory containing HelloWorld1.exe and type HelloWorld1 at the command line.
  4. Select Run from the Windows Start menu. Type HelloWorld1 preceded by the full path to the .EXE file, and choose OK.

Any of these four techniques generate the "Hello, world" output.

NOTE
When running the program using option 1, option 2, or option 4, Windows immediately opens an MS-DOS window in which to run the app. When the program terminates, Windows quickly closes the window. Thus, for a short program like this one, the window might not stay open long enough for you to read the output. There are ways around this problem, several of which we'll visit later on in this chapter. In the meantime, either take my word for it that the output is there or use option 3 to run the program.

Microsoft Developer Environment 6

Before continuing with more challenging apps, let's use the simple HelloWorld1 app as a way to introduce the MDE, the user interface for Visual J++. We've already seen a bit of Project Explorer and the Text editor, but even here there are a few additional features to point out. To look at these features, open the HelloWorld1 app in Visual J Plus Plus.

Multiple Document Interface and the Single Document Interface

Two fundamentally different modes are available for the MDE. In Multiple Document Interface (MDI) mode, the MDE acts as a single desktop upon which all other windows, such as Project Explorer and the Text editor, reside. In this mode, you can't see Windows apps behind the MDE desktop and MDE windows can't leave the MDE desktop. This is the mode under which previous versions of Visual J++ operated.

In Single Document Interface (SDI) mode, there is no MDE desktop. Rather, the menus, taskbar, and status bar form one window, Project Explorer resides in another window, and each Text editor occupies its own window. In this mode, the MDE windows appear on the Windows desktop in front of whatever apps might be executing in the background.

At first, SDI mode is disconcerting since the different windows seem to have a tenuous connection to each other and to the menu bar. SDI mode is especially confusing if you happen to run Visual J Plus Plus twice. When you do that, you really have no idea which windows belong to which MDE. However, once you get used to it, SDI will probably become your preferred style.

You can change from SDI to MDI and back again. To do so, choose Options from the Tools menu, expand the Environment branch and choose General, and select or deselect SDI Environment. You'll have to close and then reopen Visual J Plus Plus before the change will take effect.

Docking windows

Another feature of the MDE is docking windows. A window is docked when it is firmly attached to another window, generally to its edge. Docking applies only in MDI mode. By default, all windows—except the Text editor—have docking set on.

Docking windows takes some getting used to; learning a few basics will help. First, if you place a dockable window near another dockable window (especially near the menu or toolbars), the MDE automatically snaps them together, issuing an audible snapping sound to confirm the action.

You can try to undock the window by dragging it away; however, the window is likely to simply redock in some other configuration. A better alternative is to double-click the title bar, which automatically undocks the window and keeps it undocked. Now you are free to move the window wherever you like. Double-clicking the title bar again throws the window back to its previous docked position.

If you're like me and don't like docking windows next to each other, you can right-click the title bar to disable the docking feature and leave it undocked. Before you do that, however, I want to show you one other docking mode that I find fits my personality very well.

Sometimes a window will disappear as soon as you disable docking. Usually this is because the window is just barely outside the parent window. Before you give the window up for lost, click the maximize button on the MDE title bar and look for it, usually along the bottom of the screen. If you still can't find it, you can reenable docking for the window you're looking for by choosing the window in the Window menu and then selecting Dockable from the Window menu.

In addition to docking windows next to each other, the MDE supports docking windows on top of each other. It's a little tricky to get the first two windows to dock in this mode; you basically want to drag two similarly sized windows directly on top of each other. Once you do, the top window will reappear within a window with a tab attached. The back window is hidden except for its tab, which appears immediately beside the top window's tab.

To add another window to the tabbed group, follow these steps:

  1. Make the window visible by choosing View from the Edit menu and selecting the appropriate option.
  2. Use the mouse pointer to grab the window by the title bar.
  3. Drag the window to the tab labels.

Step 3 isn't obvious. Your first instinct might be to drag the window on top of the tabbed window group; however, this simply docks the window to the window pair.

My preference is to dock all MDE windows (except for Text editor windows) to the single tabbed window, as shown in Figure 1-3. Notice that I have widened the tabbed window to the point where each tab label is completely visible. As I shrink the window, the MDE truncates the labels more and more until it eventually removes them.

Java Click to view at full size.

Screenshot-3. A tabbed window containing most of the MDE development windows.

Text editor

The most important window the programmer uses is the Text editor; this window contains the Java source code.

Color coding

View the Class1.java source file in the Text editor and one thing jumps out at you right away. (If you've closed the Text editor, double-click Class1.java in Project Explorer to reopen it.) The Text editor color codes the different statement types, using green for comments, blue for keywords, and black for executable statements.

Indentation settings

As you type, the Text editor can help you to implement a consistent indentation style. Depending on what style settings you have selected, the editor automatically indents statements and aligns open and close braces. To adjust these features, choose Options on the Tools menu and under the Text Editor branch choose Java Format.

Smart editor

The Text editor also checks syntax as you type. As soon as it detects a syntax error, it places a squiggled red line underneath the offending statement, as shown in Figure 1-4. (This is similar to the way Microsoft Word underlines misspelled words.) You can disable syntax error checking by choosing Java Tasks under Text Editor in the Options dialog box.

Java Click to view at full size.

Screenshot-4. Smart editor syntax checking.

Statement Completion

The final feature of the Text editor I'll point out is the automatic object browser, called the Auto List Members feature. As soon as you enter a dot (".") after either a class or an object name, Auto List Members drops down a list of all the possible entries. For example, Figure 1-5 shows the Text editor after you have entered the dot following the word System.

Java Click to view at full size.

Screenshot-5. The Text editor's Auto List Members feature.

Members marked with a brick that has racing stripes (this brick is colored magenta on a color display) represent member methods and functions. Two racing bricks represent a method or function with more than one implementation. Members marked with a brick that doesn't have racing stripes (this brick is colored teal) represent data members.

That process repeats itself upon entering the dot after System.out. After you select the println() method from the list, the Parameter Information feature displays the method arguments as shown in Figure 1-6. Here you can see that I paged down through a list of the 10 println() methods defined for the PrintStream class (the class of out) until I found the one I wanted, in this case the fourth one in the list. This version of the println() method takes a String and returns a void (that is, nothing). The parameter information continues to display until I type the close parenthesis.

Java Click to view at full size.

Screenshot-6. Method arguments displayed in the Text editor.

The Statement Completion feature, which includes the Auto List Members and the Parameter Information features, extends to functions you write. As soon as you save your source file, the Text editor adds any methods you have created to the Auto List Members list. If you include /**-style Javadoc comments in front of your functions, the comments appear along with the method name. In addition, as you begin to enter the arguments to the method in the Text editor, the comments following the Javadoc @param appear.

Statement Completion, which includes the Auto List Members and the Parameter Information features, drastically reduces the number of trips you take to the Visual J Plus Plus documentation. Java is a small language with a very large class library that is made even larger by the addition of WFC. Being prompted by lists of available data members and methods, along with their arguments, is a great help in coding with Java and WFC.

Object Browser

Oddly enough, once the Statement Completion list has closed there appears to be no way to make it reappear, short of deleting the dot and retyping it. Another path to the same functionality is the Object Browser.

To use the Object Browser, you choose Other Windows from the View menu and pick Object Browser from the list. When you type System in the filter box at the top of the browser window and press Enter, the Object Browser searches through all members of all packages and returns every class that contains a reference to the word System. Notice that a number of the entries it finds are not what we're looking for. Nonetheless, the correct System class was in the list.

When you select the System class under java.lang, its members are displayed in the members pane. Select the object that we're looking for, out, and a full description of the object is displayed at the bottom of the Object Browser, as shown in Figure 1-7.

Screenshot

Screenshot-7. The Object Browser enables you to look for and get detailed class information.

Task List

The Task List window is another extension of the Text editor. The MDE searches the comments in Java source files that you are editing for particular keywords. For example, the various wizards put the phrase TODO: in places where they think you need to add code to complete your program.

When the environment finds these keywords, it adds them to the Task List. Thus, the Task List is an ongoing list of what is left to be done. Figure 1-8 shows the Task List items for our HelloWorld1 app.

Java Click to view at full size.

Screenshot-8. The Task List resulting from the HelloWorld1 app, showing one task left to be done.

Double-clicking the TODO item takes you to the proper line within the Java source code. (Notice that the TODO shown in Figure 1-8 was left only because I did not edit it out when I added the println() code.)

The Task List contains more than just TODO entries. You are free to add your own entries to the list of comment keywords that the environment is searching for. Compiler errors are added to the Task List as well. In addition, you can display the different types of entries in the Task List independently by right-clicking the Task List window and selecting the type of task you would like to see.

(I don't know if it's just me, but I find the name of the Task List confusing. I keep expecting to see a list of the tasks executing under the operating system rather than the tasks left for me to do.)

Toolbar customization

In Visual J Plus Plus, you can customize a toolbar and create new toolbars. To customize a toolbar, first make sure that the toolbar you want to edit is visible by choosing Toolbars from the View menu and selecting the toolbars you want to see. (You can also deselect the toolbars you don't want to see.)

To edit a toolbar, choose Toolbars on the View menu and then select Customize. Once the Customize dialog box appears, the toolbars are in edit mode and you can get rid of a tool by simply dragging it off the toolbar. To add a tool to a toolbar, select the Commands tab and find the menu command you want to add. If the command has an icon, drag it from the dialog box to where you want it on the toolbar as shown in Figure 1-9.

Screenshot

Screenshot-9. Preparing to add the Insert/Remove Breakpoint tool to the Standard toolbar.

If the menu command you want to put on a toolbar doesn't have an icon associated with it, you can add it to the toolbar anyway. For example, the Close command on the File menu doesn't have an icon. Dragging this command to the toolbar produces the not-so-pleasing result shown in Figure 1-10. (Notice the word "Close" between the disk-like icons.)

You can assign a tool an icon from a list of standard icons. With the Customize dialog box still open, click the Close tool with the right mouse button. This will display a list of properties that you can set for the icon. Select Change Button Image and a list of standard icons that you can use is displayed. Select the one that appeals to you the most. Figure 1-11 shows my selection.

Java Click to view at full size.

Screenshot-10. Adding the Close command initially results in the command's name being on the toolbar.

Java Click to view at full size.

Screenshot-11. Selecting an icon for the Close command that was added to the Standard toolbar.

Once you've selected an image, both the command name and the image appear on the toolbar. You might leave it that way for awhile, until you've become accustomed to what the image stands for. Eventually you'll want to reclaim the precious toolbar territory by getting rid of the word. To do so, open the Customize dialog box again, right-click the tool, and select Default Style. This will remove the command label, leaving only the icon.

On a toolbar, groups of tools are separated by vertical chisel lines. To add a chisel line, you right-click the icon that is to the right of where you want the chisel line. From the drop-down menu, select Begin a Group.

Each of the toolbars can be edited independently. However, my preference is to edit one toolbar (generally the Standard toolbar), add all of the tools I find most useful, and leave it visible all the time. Comments