Writing code by hand

For the most part, as you have progressed through this Getting Started document, you have been using the Visual Composition Editor and the SmartGuides to generate Java code for you, or you have been adding sections of code that we have provided for you. When you create your own applications, you will likely need to write sections of code by hand, in the Source panes of the IDE browsers. VisualAge for Java provides several tools to help you write correct, neat code by hand. This section describes some of these tools and then presents a simple handcoding scenario.

Code Assist

Source panes and some other dialogs and browsers (for example, the Configure Breakpoints and Scrapbook windows) contain Code Assist, a tool to help you find the classes, methods, and fields you are looking for without having to refer to class library reference information. Code assist is accessed by typing Ctrl+Spacebar.

When you type Ctrl+Spacebar, classes methods, and types that could be inserted in the code at the cursor are shown in a pop-up list, from which you can select one. Code Assist performs a visibility check and classes, methods, and fields that are not visible are not displayed. If the Code Assist mechanism cannot find a member that fits the current location of the cursor, the information line at the bottom of the pane will indicate that no Code Assist is available for the current context.

Code Assist for types

To insert the name of a class or interface in your code, enter the first one or more letters of the type name, and then type Ctrl+Spacebar. A pop-up list appears, containing types that start with what you have entered. Enter more letters to narrow down the list. Select an item to insert it into your code at the cursor. If the type needs to be qualified, the qualification is also automatically inserted.

Example: Create a test project and package. In the test package, create a class called AssistTest. In the AssistTest class, create a method called assistMethod. Suppose you want to declare a local Integer variable, i. In the body of the assistMethod source, type the following letters: In

Type Ctrl+Spacebar. The pop-up list of options appears.
Source pane, showing Code Assist for types

The list of available types is long. To find Integer, enter the letters te. Now, Integer is near the top of the list.
Source pane, showing Code Assist selection of Integer

Select it using the arrow keys and press Enter.

Now finish the declaration so that the method looks like this:

public void assistMethod() {
 Integer i;
}

Save the method by typing Ctrl+S. You will use this test method in the next example.

Code Assist for methods and fields

Code Assist can also list the methods and fields available for an object or class. Enter the name of the object, a period (.), and a few letters from the start of the method or field name; then type Ctrl+Spacebar. The list of methods and fields for the object will pop-up. Select one to insert it in the code.

Example: In the assistMethod method you created in the previous example, below the line that declares i , enter the following code (the period is important):

i = Integer.

Type Ctrl+Spacebar. A list of methods and fields for the Integer class pops up.
Source pane, showing methods and fields associated with Integer

Enter the letters val until you find valueOf(String) Integer. The parameter types (in this case, String) and return type (Integer) are shown.
Source pane, showing Code Assist selection of valueOf(String) Integer

Select valueOf(String) Integer and press Enter; it is inserted into your code. Enter a string such as "35" between the parentheses and end the line with a semicolon. The method source will now look like this:

public void assistMethod() {
 Integer i;
 i = Integer.valueOf("35");
}

If you request Code Assist for a method or field from a class that requires qualification, the class must be qualified before you type Ctrl+Spacebar. Otherwise, no Code Assist will be available. Generally, the code that appears before the cursor must be compilable before you request Code Assist.

Example: Suppose java.util.* is not in your class import statement. This means that the class ResourceBundle must be qualified when you use it in your class. If you type the following code, and then type Ctrl+Spacebar to get the list of methods available, no list will be available:

public String newMethod ( ) {
ResourceBundle a = ResourceBundle.
  // place cursor after period
  // and type Ctrl+Spacebar

However, if you type the following code, where the class qualification is provided, Code Assist is available:

public String newMethod ( ) {
ResourceBundle a = java.util.ResourceBundle.
  // place cursor after period
  // and type Ctrl+Spacebar

An easier way to produce a qualified name in this case (assuming you do not want to add this class or package to the import list) is to place the cursor before the period and type Ctrl+Spacebar. Select the class name from the list and it will be fully qualified for you automatically. Then type the period and Ctrl+Spacebar. The list of methods in ResourceBundle will now pop-up.

Code Assist for method parameters

Code Assist includes pop-up help for method parameters. For example, when you select valueOf(String) Integer from the pop-up list in an example, above, the following text is inserted at the cursor:

valueOf()

The cursor is automatically placed between the parentheses, and the pop-up label String appears to let you know what type of parameter to add.
Source pane, showing Code Assist for parameter

Customizing Code Assist

You can customize Code Assist behavior from the Options window, as follows. For more information about these options, see the online help.

Code Assist Tips

Code clues

If you try to save code that contains an error, the IDE warns you that the code has an error. If it can determine the type of error, it will present a list of possible solutions. You can select one and correct the error, or you can save the code with the error (it will be added to the list of problems on the Problem page of the IDE browsers that contain the program element).

For example, add the following line (including the mistake) to the assistMethod method from above:

System.out.pritn(i);

When you save the method, the following dialog will appear, suggesting alternative code that will fix the problem:
Warning window, showing possible replacements for mistyped print() method

Select the suggested correction print(Object) void and click Correct. The method will be saved with the replacement code. If you click Save, the method will be saved with the error. If you click Cancel, the method will not be saved and the error will remain in the code.

Code formatting

To promote neat, easy-to-read coding, the IDE provides an automatic code formatter which automatically controls how your code appears when you write it in a Source pane. To set code formatter options, including indentation and new-line controls:

  1. Open the Options dialog by selecting Options from the Window menu.
  2. In the left-hand list in the Options dialog, expand the Coding item.
  3. Select the Formatter item. On the Formatter page you can enable options that tell the Source panes to start a new line for each statement in a compound statement, or to use and opening brace.
  4. Select the Indentation item. On the Indentation page, you can select an indentation style.

These specifications are applied automatically to all new code. If you have imported code from the file system, or if you change the formatting options, you can apply the options to code in a particular source pane by selecting Format Code from the pane's pop-up menu.

Writing a simple bean by hand

Although visually composing beans in the Visual Composition Editor is often faster than writing them from scratch, you do have the option of writing beans by hand. The steps below show you how to write a very simple applet by hand. Compare this process with the visual composition example starting at Creating an applet, a project, and a package.

From the Workbench File menu, select Quick Start. Then from the Quick Start window, follow these steps:

  1. Select Basic and then Create Class.
  2. Select OK. The Create Class SmartGuide opens.

In the Create Class SmartGuide, follow these steps to create your applet:

  1. In the Project field, type a project name.
  2. In the Package field, type a package name.
  3. In the Applet name field, type a name. (For this example, we will use HelloWorld.)
  4. In the Superclass field, select JApplet.
  5. Make sure that Browse class when finished has been selected.
  6. Select Finish.

The class browser for HelloWorld appears, displaying the following in the Source pane:

/**
 * This type was created in VisualAge.
 */
public class HelloWorld extends com.sun.java.swing.JApplet {
}

Add import statements for your class above the generated comment, like this:

import com.sun.java.swing.*;
import java.awt.event.*;
import java.awt.*;
/**
 * This type was created in VisualAge.
 */
public class HelloWorld extends com.sun.java.swing.JApplet {
}

Save the new code by pressing Ctrl+S.

From the Methods pane, create an init() method to complete this applet:

  1. Click mouse button 2; select Add and then Method. The Create Method SmartGuide appears.
  2. Change the highlighted text to void init()
  3. Select Finish. The generated stub for init() appears in the Source pane of the class browser.

Write code for the new method. For this example, add code into the stub so that it looks like this:

public void init() {
   JButton button = new JButton("Hello World!");
   setBackground(Color.lightGray);
   getContentPane().add(button, "Center");
}

Save the new code by pressing Ctrl+S.

If you were to run HelloWorld at this point, the applet viewer would not be able to initialize the applet. The last step is to set the correct class path:

  1. From the Class menu, select Properties. A properties window opens.
  2. From the Class Path page, select Compute Now and then OK.

Now run the class. It is simple, but it works!