Working with TextPad

TextPad is an inexpensive ($32) text editor that you can integrate with the Java JDK to simplify the task of coding, compiling, and running Java programs. It isn't a true Integrated Development Environment (IDE), as it lacks features such as integrated debugging, code generators, or drag-and-drop tools for creating graphical user interfaces. If you want to work with an IDE, I suggest you skip this chapter and instead look to Book I, Chapter 4, which covers a free IDE called Eclipse.

TextPad is a popular tool for developing Java programs because of its simplicity and speed. It's ideal for learning Java because it doesn't generate any code for you. Writing every line of code yourself may seem like a bother, but the exercise pays off in the long run because you have a better understanding of how Java works.

Downloading and Installing TextPad

You can download a free evaluation version of TextPad from Helios Software Systems at http://www.textpad.com. You can use the evaluation version free of charge, but if you decide to keep the program, you must pay for it. (Helios accepts credit-card payment online.)

If the Java JDK is already installed on your computer when you install TextPad, TextPad automatically configures itself to compile and run Java programs. If you install the JDK after you install TextPad, you need to configure TextPad for Java. Follow these steps:

  1. Choose Configure image from book Preferences.
  2. Click Tools in the tree that appears at the left of the Preferences dialog box.
  3. Click the Add button to reveal a drop-down list of options, and then click Java SDK Commands.

    Figure 3-1 shows how the Preferences dialog box appears when the Java tools are installed. As you can see, the Tools item in the tree at the left of the dialog box includes three Java tools: Compile Java, Run Java Application, and Run Java Applet.

    image from book
    Figure 3-1: Configuring tools in TextPad.

  4. Click OK.

The commands to compile and run Java programs are added to TextPad's Tools menu.


Editing Source Files

Figure 3-2 shows TextPad editing a Java source file. If you've worked with a Windows text editor before, you'll have no trouble learning the basics of using TextPad. I won't go over such basic procedures as opening and saving files because they're standard. Instead, the following paragraphs describe some of TextPad's features that are useful for editing Java program files.

image from book
Figure 3-2: Editing a Java file in TextPad.

  Tip 

When you first create a file (by clicking the New button on the toolbar or by choosing File image from book New), TextPad treats the file as a normal text file, not as a Java program file. After you save the file (click the Save button or choose File image from book Save) and assign java as the file extension, TextPad's Java-editing features kick in.

The following paragraphs describe some of TextPad's more noteworthy features for working with Java files:

  • You can't really tell from Figure 3-2, but TextPad uses different colors to indicate the function of each word or symbol in the program. Brackets are red so you spot them quickly and make sure they're paired correctly. Keywords are blue. Comments and string literals are green. Other text, such as variable or method names, show as black.
  • TextPad automatically indents whenever you type an opening bracket, and then reverts to the previous indent when you type a closing bracket. Keeping your code lined up is easy.
  • Line numbers display down the left edge of the editing window. You can turn these line numbers on or off by choosing View image from book Line Numbers.
  • To go to a particular line, press Ctrl+G to bring up the Go To dialog box. Make sure Line is selected in the Go to What box, enter the line number in the text box, and click OK.
  • If you have more than one file open, you can switch between the files by using the Document Selector, the pane on the left side of the TextPad window. If the Document Selector isn't visible, choose View image from book Document Selector to summon it.
  • Another way to switch between two (or more) files is to choose View image from book Document Tabs. The tabs at the bottom of the document window display. You can click these tabs to switch documents.
  • A handy Match Bracket feature lets you pair up brackets, braces, and parentheses. To use this feature, move the insertion point to a bracket. Then press Ctrl+M. TextPad finds the matching bracket.
  • To search for text, press F5. In the Find dialog box, enter the text you're looking for and click OK. To repeat the search, press Ctrl+F.
  • To replace text, press F8.

Using workspaces

In TextPad, a workspace is a collection of files that you work on together. Workspaces are useful for projects that involve more than just one file. When you open a workspace, TextPad opens all the files in the workspace. And you can configure TextPad so the last workspace you were working on opens automatically whenever TextPad starts.

To create a workspace, first open all the files that you want to be a part of the workspace. Then, choose File image from book Workspace image from book Save As and give a name to the workspace. (The list of files that make up the workspace is saved in a file with the tws extension.)

To open a workspace, choose File image from book Workspace image from book Open. Then, select the workspace file you previously saved and click Open. Or choose the workspace from the list of recently used workspaces (the list appears at the bottom of the File image from book Workspace menu).

To configure TextPad to automatically open the most recently used workspace whenever you start TextPad, choose Configure image from book Preferences. Click General in the preferences tree at the left of the dialog box, and then check the Reload Last Workspace at Startup option and click OK to close the Preferences dialog box.


Compiling a Program

To compile a Java program in TextPad, choose Tools Compile Java or use the keyboard shortcut Ctrl+1. The javac command launches in a separate command prompt window and displays the compiler output to a separate Command Results window. If the program compiles successfully, TextPad returns immediately to the source program. But if the compiler finds something wrong with your program, the Command Results window stays open, as shown in Figure 3-3.

image from book
Figure 3-3: Error messages displayed by the Java compiler.

In this example, the following three compiler error messages are displayed:

J:Book1Ch04HelloApp.java:10: ')' expected
 System.out.println("Hello, + greetee + "!");
 ^
J:Book1Ch04HelloApp.java:10: unclosed string literal
 System.out.println("Hello, + greetee + "!");
 ^
J:Book1Ch04HelloApp.java:11: ';' expected
 }
 ^
3 errors

Tool completed with exit code 1
  Tip 

If you double-click the first line of each error message, TextPad takes you to the spot where the error occurred. For example, if you double-click the line with the unclosed string literal message, you're taken to line 10, and the insertion point is positioned on the last quotation mark on the line, right where the compiler found the error. Then, you can correct the error and recompile the program.

  Tip 

Often, a single error can cause more than one error message to display. That's the case here. The error is that I left off a closing quotation mark after the word Hello in line 10. That one error caused all three error messages.


Running a Java Program

After you compile a Java program with no errors, you can run it by choosing Tools image from book Run Java Application or pressing Ctrl+2. A command window opens, in which the program runs. For example, Figure 3-4 shows the HelloApp program running in a separate window atop the TextPad window.

image from book
Figure 3-4: Running a program.

When the program finishes, the message Press any key to continue displays in the command window. When you press a key, the window closes and TextPad comes back to life.

  TECHNICAL STAUFF 

In case you're wondering, TextPad actually runs your program by creating and running a batch file-a short text file that contains the commands necessary to run your program. This batch file is given a cryptic name, such as tp02a11c.BAT. Here's the batch file generated for the HelloApp program:

@ECHO OFF
C:
CD Book1Ch04
"C:Program FilesJavajdk1.6.0injava.exe" HelloApp
PAUSE

Here's a closer look at these commands:

  • The first command tells MS-DOS not to display the commands in the command window as the batch file executes.
  • The next two commands switch to the drive and directory that contains the java program.
  • Next, the java.exe program is called to run the HelloApp class.
  • And finally, a PAUSE command executes. That's what displays the Press any key to continue message when the program finishes.


Running an Applet

You can also run an applet directly from TextPad. First, compile the program. Then, if the program contains no errors, choose Tools image from book Run Java Applet or press Ctrl+3. A command window appears. Then, the Java applet viewer is started. It runs the applet in a separate window, without the need for a Web browser. Figure 3-5 shows an applet in action.

image from book
Figure 3-5: Running an applet.

When you quit the applet, the Applet Viewer window and the DOS command window close and you return to TextPad.


Book I - Java Basics

Book II - Programming Basics

Book III - Object-Oriented Programming

Book IV - Strings, Arrays, and Collections

Book V - Programming Techniques

Book VI - Swing

Book VII - Web Programming

Book VIII - Files and Databases

Book IX - Fun and Games



Java All-In-One Desk Reference For Dummies
Java All-In-One Desk Reference For Dummies
ISBN: 0470124512
EAN: 2147483647
Year: 2004
Pages: 332

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net