Ask Questions?

View Latest Questions



NetBeans IDE
Posted on: July 26, 2006 at 12:00 AM
NetBeans is a free, open-source, IDE which is available at This is a reasonable choice, and liked by many programmers.

Java Notes

NetBeans IDE


  • Introduction to NetBeans
  • Downloading NetBeans
  • Where to find tutorials and documentation
  • Starting a new project in NetBeans
  • Starting a new GUI project (covered in the tutorial)
  • Misc settings in NetBeans you might want to change
  • Other features you might want to use


[Notes written for NetBeans 5.0 rc 2 (better than the official 4.1)]

NetBeans is a free, open-source, IDE which is available at This is a reasonable choice, and liked by many programmers. It isn't as full featured as IntelliJ IDEA (expensive) or Eclipse (free), but it has other advantages: cheaper in the case of IntelliJ IDEA, and a good GUI form editor, which appears not to be as useful in Eclipse. Overall a good choice.

Immediate error messages. The most useful feature for students is the jagged red line put under erroneous statements when there's a pause in the typing. NetBeans continuously compiles the program as you're typing it to alert you whenever there's an error. It's like the continuous spell checking of some word processors. I've gotten so used to this feature that I feel uneasy when I use an editor that doesn't do it.

Indentation/braces is a serious problem for beginning students. Just right click the source code and choose Reformat Code. It will reformat the entire file if nothing is selected, otherwise only the selected text. It will also show the matching brace when the cursor is beside a brace.

Renaming is one of the most common operations, and NetBeans makes this easy -- even when renaming a variable like "a" because it parses the text and doesn't just use a simple textual match.

Downloading NetBeans

You must install the Java JDK before installing NetBeans. Go to and download J2SE 5.0 (or more recent version), if you don't already have it. Sometimes there is a link to a bundled version of the JDK and NetBeans, which, if a available, can be used to install both at the same time. Otherwise install the JDK and then download and install NetBeans, which can be downloaded from

Where to find tutorials and documentation

Check for the most current documentation and recent articles.

Starting a new project in NetBeans

  1. Click New Project in File menu or Welcome window.
  2. Choose Category General, and Project Java Application (probably the defaults). Click Next.
  3. The New Java Application dialog box that appears is surprisingly awkward to use, but it is asking from some simple information: the name of the project, the directory you want to put the project in, and the name of your "main" class, if there is one. It's made more confusing than necessary because of the way NetBeans tries to help you by making (often bad) default choices for you. After you use it a few times, it should become simple.
    • Choose a Project Name. This is not only the name of the project, but it will be used to create a directory/folder by that name, and also, after mangling the name, it will use it as the name of the package. It starts with an unhelpful name like "JavaApplication" - change it.

      Just to test it, try typing something in upper case with spaces in it, for example "X Y" (without the quotes). You'll notice in the Project Folder text field that it plans to create a folder with that name. In addition, in the last, unnamed, text field there is the same text turned to lower case, without blanks, followed by ".Main". This lower case, deblanked version will be the package name.

      Choosing a lower case no-blank name for the project name is probably the least confusing to start with. That way, the project, the folder, and the package will all have the same name.

    • Choose a Project Location. This is the folder in which your project folder will be created. You can have many projects in this folder, so it would be best to create a folder that you will use for all projects for a class, perhaps something like "cmis340", using the Browse button to find and/or create the folder. The Project Folder field should now be automatically set for you using a combination of the Project Name and Project Location.
    • Package name and main class name are set in the final section of this dialog. Leave both Set as Main Project and Create Main Class checked and fill in the text field.

      The main class name is the part after the dot and defaults to the again unhelpful "Main" name. Do not use this default name. Change the name to the name you want your main program's class to have -- something more meaningful like "LeapYear". Remember that class names start with an upper case character. It's fairly easy to change class names later in NetBeans, so don't worry if you later decide on a better name. Getting class names right is important, and I often improve the names later.

      The project name is the part before the dot. It should already be set from the project name, but you don't really have to use this default package name. You can change it to any legal package name, even get rid of it entirely (and the dot) to make it a "default" package. I often use "com.fredswartz.whatever" to put my "whatever" package in an entire package hierarchy that uniquely identifies it as mine. Package names should be in lower case.

    • Click Finish.
  4. The left panel now shows your project, and the right panel shows a source file. If necessary, in the project panel expand the project, then Source Packages, then the package, and now you can see all Java source files in your project.

Starting a new GUI project in NetBeans

My notes below overlap the (better) tutorial to some extent (see above), but they also contain a little more information about automatically generated code and how to add listeners.

Matisse is the name given to the GUI builder in NetBeans. It generates code from the WYSIWYG design. This is an excellent GUI editor. For a look at issues with generating code instead of using some other representation of the GUI, take a look at the article Code generation considered harmful by Ed Burnette.

Version. I haven't tried anything anything challenging, so there may be problems I haven't uncovered, but I'm very happy with what I have used. I'm already comfortable building GUIs by hand (especially using my own layout manager), but NetBean's Matisse GUI editor is definitely faster.

Tutorials. In addition to the notes below, you can learn how to use the Matisse editor the following places.

Starting a new GUI project (covered in the tutorial)

These instructions assume the project window is in the upper left, and the properties window is in the lower right of the screen. You might find them minimized with icons along left/right screen edges.

  1. (Menu) File, New Project....
  2. (Dialog) Take the default General and Java Application. Click Next.
  3. (Dialog) Specify where you want it, and what to call the project directory. Unselect Create Main Class because you'll use the GUI class as the main class. Set as Main Project should be checked. Click Finish. This creates a project with the default package, but we'll create a project in the next step.
  4. (Project pane, right click the project) Select New, JFrame Form...
  5. (Dialog) Fill in the Class Name and Package fields. You can leave your program in the package, but it is highly recommended to create a named package. you can create multilevel packages by simply typing the the packages names separated by dots, eg. com.fredswartz.flograf . Click Finish.
  6. You now have a working program with a functioning main program and (empty) window. Try running it. The code that NetBeans generated contains a main method, so it runs and does nothing. You'll have to confirm that this is the "main class" (where the main method you want to use is located) the first time you run it.

GUI editor - Toggle between Source and Design views

At the top left of the toolbar for GUI editor there are Source and Design buttons. Click on Source to see the generated code, and Design to work on the the window's appearance.

Design view. The default view is GUI design view, which shows approximately what the window will look like. You can select GUI components from the pallette at the top right, and click on the window to place them. You can drag and stretch them to get the right layout.

Source view. This shows the code that has been generated by NetBeans to build this GUI. The code with the blue background, called "guarded text", is uneditable. The sections in white can be edited to customize the code in several ways, but especially to add data binding code to connect it to your model/logic. The parts that are uneditable are:

  • The declarations of the components, which are placed at the end, which is a bit unusual. You can change the names of components in the design view properties pane.
  • The initComponents() method, which is called from the constructor. You can edit the constructor to add your own code as necessary (eg, to initialize your model class.
  • The headers and enclosing braces of "handlers" (methods called by listeners to handle the event. You must write the code for the body of the handlers to make them do what they need to do.

There are several things that you need to do to make this into a useful program.

  • Layout components. Use the GUI editor to layout your program. This is the most fun because you can create some good looking designs very quickly, in contrast to the normal layout struggles with the clumsy Java layout managers. With a little practice you can get things to work pretty well.
  • Edit component properties. You will want to edit some of the properties of components.

    Right click component. Some of the very most common properties can be set by right clicking the component and selecting one of the options (eg, to change the text, the name of the corresponding variable, and create a listener).

    Properties pane. There are a huge number of properties, but only a few are useful. The Properties panel puts them in several categories, and you will probably only need to use the first category, if any (Properties, Other Properties, Layout, and Accessibility).

  • Listeners and handlers. If you specify that a component should respond to an event (eg, ActionPerformed for a button), the GUI editor generates an anonymous, inner-class listener, which calls a method based on the name of the component. For example, if you have button in a variable called greetingButton, when you indicate that you want an action listener by right clicking the component and selecting Events > Action, it creates a listener that calls a handler method called greetingButtonActionPerformed(). Type code for the action you want inside this method.

    Alternative - Properties pane. In the Properties window or popup there are three tabs at the top. The Events tab lets you specify which method should be called by the listener. Type in the method name and it will be created if necessary. Type your code in this method.

GUI editor - Structure of the generated source code

NetBeans automatically generates a lot of code for the GUI. Most of it can be ignored, and you only have to write the code in the event handlers, and initialize your "model" (logic) in the constructor.

  • Blue shaded areas are generated code that you can't edit.
  • Expand/collapse controls at are the beginning of each section. Just hit the "+" to expand, and the "-" to collapse the view.
  • No imports are used, so all library classes will be fully qualified (eg, javax.swing.JButton instead of just JButton as a human would write it).
  • Instance variables are declared at the end of the class, not near the beginning, as is the normal human style.
  • The constructor calls on the initComponents() method to do all the initialization. initComponents() is in the collapsed, uneditable area below the constructor.

    The constructor itself is editable, so you can put your own initializations in it. Because you should separate your model/logic from the user interface, you may create an instance of your model in the constructor, and declare an instance variable at the end to hold it. The listener methods can then use that instance variable to interact with the model.

  • The initComponents() code is generated by NetBeans, and isn't necessarily a good model for writing code yourself. If you decide to look at it, you will see several unfamiliar things. For example, GroupLayout won't appear until Java 6. Button listeners are done as anonymous, inner-class listeners, ...
  • The main method is defined near the end, just above the instance variables, and contains what might be an unfamiliar way to display the window. This is equivalent to
        YourGUIClass window = new YourGUIClass();

    This runs the initialization code on the EventQueue thread because there are some (uncommon) situations where it makes a difference. Don't bother to change it, but when you write your own programs you can use the simpler code above. Or you can use the NetBeans main style. In any case, it's probably easiest to just collapse this code and never take another look at it.

GUI editor - Connecting components to code (listeners, handlers) - Data binding

To create a button you would put the button on the JFrame, or select it if it's already there, then right click the component and select Edit Text to put the right text on the button top. To add code to handle the actionEvent that is generated when a button is clicked, do the following.

  1. Choose a good variable name. Right click the component and select Change Variable Name... to set the variable name to something meaningful.
  2. Listener. Right click the component and select Event > Action > actionPerformed. This will generate a listener, and generate the top and bottom lines of a "handler" method that is called by the listener when the button is clicked.
  3. Handler method. The name of this methods is the name of the JButton variable that you chose in a previous step, followed by "ActionPerformed", so choose a good name for the button. Write code to do what you want inside this method. Typically this code will have some interaction with the model and other components.

Misc settings in NetBeans you might want to change

To change editor settings.
  • Click the Tools > Options menu item.
  • In the icon display on the left, click Editor.
  • click on Editing > Editor Settings > Java Editor
  • In the right panel, click the Indentation tab.
  • If it isn't already done, set the following:
    • Number of Spaces per Indent: 4
    • Statement continuation Indent: 8
    • Check Expand Tabs to Spaces.
    • If you like the "Allmann" style of indentation, you can also check the Add New Line before Brace check box.
  • In the right panel, click on the Line Numbers check box. An even easier way to turn on the line number option is to right-click in the "gutter" to the left of the source listing. A pop-up menu offers a choice of turning on/off line numbers.
Changing to generic, type-safe, data structures. Deprecation warnings.
When compiling a program that uses non-generic data structures, it's possible to accidentally add an element of the wrong type. To alert you to places where this might be a problem, add
  • Right click on the project in the Projects panel at the left.
  • Select Properties at the bottom of the popup menu.
  • Click on Build > Compiling in the left panel.
  • In the Additional Compiler Options, put -Xlint:unchecked and click OK.
  • Also click Report Used of Deprecated APIs to be informed about obsolete library features you are using.
Turning runtime assertion checking on
  • Right click on the project icon in the Project panel at the left.
  • Select Properties at the bottom of the drop-down menu.
  • Click on the Run choice in the left panel.
  • In the VM Options text field at the right, enter "-ea" (without the quotes). "ea" stands for enable assertions.

Other features you might want to use

Importing packages into a project

To use other packages (eg, com.fredswartz.ezgui), NetBeans has to know where to find them. Here is the process.

  • Right click on the project in the Projects panel at the left.
  • Select Properties at the bottom of the popup menu.
  • Click on Libraries in the left panel.
  • On the right, click Add JAR/Folder....
  • Select the appropriate jar file, click Open, then OK.
NetBeans Quick Tip #6 - Abbreviations in Editor