Sections:
[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 www.netbeans.org. 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.
You must install the Java JDK before installing NetBeans. Go to java.sun.com 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 www.netbeans.org.
Check www.netbeans.org for the most current documentation and recent articles.
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.
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.
My notes below overlap the (better) www.netbeans.org 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.
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.
com.fredswartz.flograf .
Click Finish.
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:
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.
There are several things that you need to do to make this into a useful program.
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).
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.
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.
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.
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, ...
YourGUIClass window = new YourGUIClass();
window.setVisible(true);
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.
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.
-Xlint:unchecked
and click OK.Properties at the bottom of the drop-down menu.Run choice in the left panel.To use other packages (eg, com.fredswartz.ezgui), NetBeans has to know where to find them. Here is the process.