Apple Developer Connection
Advanced Search
Member Login Log In | Not a Member? Contact ADC

Developing for Java with NetBeans 4.1 on Mac OS X

NetBeans 4.1 is a complete open-source tool for programming in the Java language. Thanks to the excellent support for Java in Mac OS X, and a bit of Mac-specific tuning, using NetBeans on the Macintosh is an great experience for beginners and experts alike.

This article walks through creating and running a 'Hello World' project, and in the process demonstrates a number of features that NetBeans provides to make development faster and more fun. For a beginner to NetBeans, it should be enough to get you going; and even an experienced NetBeans user should be able to learn from some of the tips and tricks discussed in this article.

Getting Started

First, download NetBeans and install on your Macintosh.

The first time you start NetBeans, the Welcome Screen is opened in the editor area. The Welcome Screen contains links to documentation and a shortcut for creating a new project. If you close it, it can be redisplayed at any time by choosing Help | Welcome Screen.

Creating a Project

Click the New Project button on the Welcome Screen to create a new project (you can access this any time from File | New Project).

The New Project Wizard appears as shown in Figure 1.

The New Project Wizard


Figure 1: The New Project Wizard.

The New Project Wizard contains a variety of project types—including sample projects that demonstrate how to write various types of programs. Choose the General category, and Java Application on the right-hand side to create a simple Java application, and press the Next button to go on to the next pane of the wizard.

The New Project Wizard pane 2


Figure 2: The New Project Wizard pane 2.

Here you can provide a name and location for your new project. For this case, simply enter FirstProject as the name. The project will be created in your home folder, as a subfolder of it. Press the Finish button to complete the wizard and create the project.

The first time a project is created, NetBeans will scan your JDK and build its database, which will be used later in code completion, so be sure to wait while this task completes, which usually takes a few minutes.

Building the Code Completion Database

Figure 3: Building the Code Completion Database.

Once this task completes, you will find the class firstProject.Main open in the editor. Notice the main method is empty:

public static void main(String[] args) {
        // TODO code application logic here<
    }

Now open the To-Do List window, by choosing Window | To-Do List, as shown in Figure 4. You'll find an item in the list, corresponding to this comment. When developing, //TODO ... comments can be a handy way to keep track of things to do.

The To-Do List

Figure 4: The To-Do List.

There are other plug-ins for the To-Do List, such as PMD, that can perform code reviews and other analysis tasks and add additional items to the list. These can be downloaded via the Update Center by choosing Tools | Update Center from the main menu.

Now, let's replace the comment with a command to print the text "Hello World" to the standard output—which provides an opportunity to point out a few conveniences in NetBeans that make coding faster and more fun. So we'll walk through this step-by-step:

  1. Click anywhere in the TODO comment
  2. Press Shift-ENTER. A new line is added below this one and the caret placed there.
  3. Type the following characters: sout and press the space bar. This is an abbreviation—it is instantly expanded to System.out.println ("")—the caret is placed between the quotes.
    Note: you can find all the available abbreviations and add/remove them via Tools | Options | Editing | Editor Settings | Java Editor | Abbreviations.
  4. Type Hello World into the quoted text
  5. Press the up-arrow to return to the TODO line and press Command-E to delete the line. The result should look like this:
       public static void main(String[] args) {
            System.out.println("Hello world");
        }
       
  6. Press F6 to compile and run FirstProject. The Output Window appears, showing compilation progress, and then displaying the actual output of the program, "Hello World", as shown in Figure 5.

The Output Window

Figure 5: The Output Window Showing "Hello World."

Managing Windows

At this point, you've got several windows open that weren't open when NetBeans was started. NetBeans offers several conveniences for managing windows:
  • Window Drag & Drop—You can drag windows between areas of the main window to rearrange them. Just drag a window's tab with the mouse.
  • Editor Maximize—Double click any tab in the editor to hide all other windows, or press Shift-Escape
  • Editor Close Controls—Click the X button of any editor tab to close just that tab; Shift-Click it to close all open editor tabs; Alt-Click it to close every tab except the one you clicked.
  • Floating Windows—when you move the mouse into the top of any non-editor tab, window controls appear:
    Window Controls
    The leftmost icon is the pin control. Press it, and the output window disappears, replaced by a button labelled Output at the bottom of the screen. Simply hover the mouse over this button to re-show the output window at any time, as shown in Figure 6.

The Output Window in Floating Mode

Figure 6: The Output Window in Floating Mode.

Other Conveniences in NetBeans

The value of using an IDE like NetBeans is in how it helps to make development faster and more fun—how it saves you work. So at this point, we'll run through some of the other features that are available at the press of a key or click of a mouse that save time and typing.
  • The Navigator Window—the Navigator sits below the Files and Projects tabs by default. It shows a list of all the fields, methods and constructors in the class being edited. Double click any method to navigate there in the Editor. Additionally, you can use it to rearrange items in your classes—right-click and choose Sort by Source. Now, you can simply drag and drop methods or fields to rearrange their order in your source code.

    The Navigator Window

    Figure 7: The Navigator Window.

  • Code Completion—the Code Completion popup appears after you type a . character and press Ctrl-Space, with a list of methods and fields you can call on whatever object is being invoked. The Code Completion Popup

    Figure 8: The Code Completion Popup.

  • Test Generation & Runningtest driven development is changing the way people write software. Unit tests provably improve the reliability of software. NetBeans makes writing and using tests easy—just right-click a class in the Files or Projects tab and choose Tools | JUnit Tests | Generate Tests. You can run tests for an individual file any time by pressing Command-F6, or run all tests for a project by pressing Ctrl-F6.
  • The Favorites Window—not open by default, the Favorites Window can be opened from the Window menu. This is a handy way to work with files outside of any project—in fact, that is the way this article is being written (NetBeans has excellent HTML editing support). Right-click the root node and choose folders to add to the Favorites Window, or right-click any folder and add it from the Tools submenu. The Favorites Window is also useful if there are certain packages in your projects which you work in a lot, as a way to quickly access them.

Some other features that are very useful are as follows:

Feature Description Key Combination
Fast Open Get a list of known classes with autocomplete to quickly open another file

Ctrl-Shift-O

Fix Imports After adding a reference to a java class, automatically adds that class to the list of imported classes. Ctrl-Shift-F
Word Matching Type the first few letters of any word used somewhere else in the source file and cycle through all possible matches Command-L/
Command-K
Comment/Uncomment Comments out lines by prepending // to them Command-Shift-T/
Command-Shift-D
Create Bookmark/
Go to Bookmark
Remember/quickly navigate between spots in a file Ctrl-F2, F2
Explore File Select the currently edited file in the Projects tab, expanding folders as needed Command-Shift-1
Go to Declaration Go to the declaration of whatever field or method call the caret is in, opening the file if need-be. Ctrl-G
Jump Lists Quickly switch between recently edited positions in one or more filesCtrl-K, Ctrl-L,
Ctrl-Shift-K, Ctrl-Shift-L

Common Keyboard Shortcuts

Here are the most common keyboard shortcuts you'll use in NetBeans:

Command Shortcut
Compile F11
Run F6
Debug F5
Step F7
Run to Cursor F4
Find, Find Next, Replace Command-F, Command-G, Command-R
Next/Previous Error F12/Shift-F12
Test Project Ctrl-F6

Things That Are Cool but Easy-to-Miss

Here's a grab bag of additional tips and tricks that you'll want to know, but may not come across unless you know they are there.

  • Scroll the tabs in the editor with the mouse wheel
    Try it when you have a lot of files open
  • Reorder tabs by dragging them to different positions
  • Wrap text in the output window
    Right-click and choose Wrap Text from the contextual menu
  • Drag and drop files between packages and have them be automatically refactored
  • Debug your unit tests
    Command-Shift-F6
  • Fix-and-continue ("hot swap") in the debugger
    Change your code while debugging and choose Run | Fix
  • Watch expressions
    just create a new Watch when debugging, such as x * 100 and the value of the expression will appear as the watched value
  • Create menu/toolbar items for Ant targets
    Expand an Ant script, right-click a target and choose Create Shortcut
  • Use code completion to browse javadoc
    Just position the caret in an existing method call and press Ctrl-Space to invoke code completion, and read the javadoc for the method being called in the popup
  • Quick Search
    Give focus to a tree control, and simply start typing a name to select a file that matches (use Tab to toggle if multiple files match). Press Escape to hide the quick search popup.

The NetBeans Open Source Project

The NetBeans Open Source Project is sponsored by Sun Microsystems, the creators of the Java language. NetBeans is the basis of Sun's commercial Java development tools, Java Studio Creator and Java Studio Enterprise. So if you're using either of these products, you're using NetBeans.

Ant-Based Projects

All projects in NetBeans use Apache Ant to compile and run them (that's what produces the initial output in the Output Window screen shot shown in Figure 5 above). So a "project" is a directory, an ant script and whatever source or other files are below that directory. That means that all NetBeans projects can be built from the command-line, outside NetBeans; and that NetBeans projects can be shared via version control systems such as CVS, making it the ideal tool for collaborative and open-source software development.

The NetBeans Community

NetBeans is the product of an open source project, and there's a lively community of users and developers; in particular, the nbusers mailing list is a valuable resource for getting answers to questions. You can either subscribe to the list, or access it via a news reader from news.gmane.org.

Posted: 2005-05-16