oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button Java Programming on the Mac

Building a Simple Java Application in Mac OS X

by Daniel H. Steinberg

One of the cool things about writing a column on Java for Mac OS X is that there are many different types of people interested in the subject. Two of the biggest divisions are Java developers who are interested in what Mac OS X has to offer, and developers interested in creating Mac OS X applications who think that Java might be the right vehicle.

Last month's column addressed the first group. The goal of that article was for Windows developers and Unix experts to stop and say, "I didn't know you could do that on a Mac." If you are in this first group, you may want to skip to the end of this piece and play with this month's challenge.

In this article, we'll take things a little bit more slowly and create a simple Java application that doesn't do very much at all. This will allow you to see the basic steps needed for getting a simple GUI application up and running. This is not an intro to Java or object-oriented programming, but I will take time to discuss some of the start-up issues and point you in the direction of other references.

The current plan for this column is to alternate between the two target groups. As always, if there are topics you'd like to see covered or any thoughts about future articles, drop me a line at

Note: Apple has posted a developers' preview of Java 1.3.1 on the ADC. Even free members are entitled to download the upgrade and the documentation. Check it out. Borland recommends that you have this version installed before using its JBuilder 5 beta version for Mac OS X.

Getting started

Comment on this articleTry building this application and let us know how it went.
Post your comments

Also in Java Programming on the Mac:

A Rendezvous with Java

Integrating Ant with Xcode

Transforming iCal Calendars with Java

Apple Releases Java 1.4.1 for Mac OS X

Getting Fit for the Holidays

First create a directory inside your user directory. You can do this by opening up the Finder and navigating to

Mac OS X/Users/<user name>/

In my case, my user name is "dhs" so I will create a new folder named "NineSquares" inside of Mac OS X/Users/dhs/. Now open up the Terminal application. As a reminder, you can find this in Mac OS X/Applications/Utilities/. You may find it useful to drag Terminal to your dock. When Terminal starts up you should see a prompt that looks something like

[localhost:~] <user name>%

Type in the command ls and you will get a listing of the contents of the current directory. You should see NineSquares in the list.

Now we need a simple text editor. You can use emacs, vi, BBEdit, Alpha, or your personal favorite text editor. As your programs get more complicated, it's nice to have a text editor that does context coloring for you and perhaps matches brackets. If you want a smarter editor that does code completion (for example, suggesting the methods that are available to an object), you may want to check out one of the IDEs now available on the Mac. In this article, I'll just use Text Edit. It's free. It comes with Mac OS X and there's nothing really to learn or install.

Start up Text Edit. You'll find it in Mac OS X/Applications. We aren't going to want to save this as Rich Text so select "Make Plain Text" from the Format menu. Now choose "Save As" from the File menu and navigate to the NineSquares folder you just created. Name the file and click "Save". Text Edit wants to save this as a .txt file but it knows that something may be wrong and it presents you with the following warning.

Screen shot.

Select "Don't append" and you will have created and saved the appropriately named empty document Now go ahead and type in the following.

package NineSquares;

import javax.swing.JFrame;

public class NineSquares {
 public static void main( String args[]) {
  JFrame myFrame = new JFrame("Nine Squares");

The execution of the program will begin with the body of the main() method. In this case, we're creating a new JFrame named myFrame that will have "Nine Squares" in the title bar. It would be nice to see what we've created, so we need to make myFrame visible.

The third step ensures that the red "close" button behaves properly. We don't just want the red button to close the window we're creating -- we want it to quit our application. So we set the default close operation to EXIT_ON_CLOSE. There's no way you can keep everything in your head when you're programming. You're going to forget what a method is called or what arguments it takes. Use the JavaDocs that come with the Java installation. You'll find them at: file://localhost/System/Library/Frameworks/ JavaVM.framework/Versions/A/Resources/English.lproj /Documentation/Java/index.html.

For example, to find out what's available to you in JFrame navigate down the list in the lower left until you get to it. Click on it and you will see a complete description in the lower-right corner. By following the links you can usually answer the questions you have.

Back to our first program.... If you tried compiling and running with just the class declaration and the main() method it contains, you would get a complaint from the compiler that it has no idea what you mean by JFrame. You need the import statement to acquire the information about JFrame.

You could have imported the entire javax.swing package with the command import javax.swing.*. I tend to be specific about what I'm importing. This used to be recommended for performance reasons. Although performance is no longer affected, I like to do this so I know the dependencies of this class. It also helps me when I'm cleaning up after removing a reference or two.

The final addition to is the package declaration. I'm going to end up with several files so I'd like to keep them all in a single package. Physically, this means that I will keep them all in the directory NineSquares, but programmatically I'd also like to think of them as a unit.

To compile your file, go to the Terminal application and type javac NineSquares/ Then hit return. This indicates that you want to compile the file inside the NineSquares directory. The next prompt should appear and now you can run your application by typing java NineSquares/NineSquares. A small window will appear. Resize the window and drag it around -- press the green, yellow, and red buttons and you'll see that you get the proper behavior of a window for free.

Pages: 1, 2, 3

Next Pagearrow