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.
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:
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.
Figure 3-1: Configuring tools in TextPad.
The commands to compile and run Java programs are added to TextPad's Tools menu.
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.
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 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 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:
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 Workspace 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 Workspace 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 Workspace menu).
To configure TextPad to automatically open the most recently used workspace whenever you start TextPad, choose Configure 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.
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.
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. |
After you compile a Java program with no errors, you can run it by choosing Tools 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.
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:
You can also run an applet directly from TextPad. First, compile the program. Then, if the program contains no errors, choose Tools 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.
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