This guide demonstrates the process of creating and editing Java sources using the new and improved NetBeans IDE 6.0 Editor.
Note: Click the Arrow button in the center of each video to start playback. Each video contains on-screen text identifying important procedures and concepts which automatically pause playback. To resume playback of the videos, simply click the displayed on-screen text or use the controller below the video.
Before you begin, you need to download or install the following software on your computer:
In this section, we'll take a look at the new Java editor features that enable you to be more productive when writing code. These features include code generation using Code Completion and the Code Generation dialog, maintaining import statements, as well as using advanced tools for manipulating lines and selected code blocks.
Generally speaking, Code Completion is useful for filling in missing code, such as symbols and keywords, automatically. As of NetBeans IDE 6.0 you can now also use Code Completion to generate whole methods. This is useful if you want, for example, to generate a constructor, override methods, or implement methods. Later in the tutorial, we'll also take a look at some other interesting new features of NetBeans 6.0's Code Completion, such as generating arbitrary constructors using the Code Generation dialog.
The first thing we will look at is the writing of a JavaBean representing an email message. The bean skeleton containing the private fields needed for storing the data has already been prepared for us so we now need to create a reasonable constructor for it, as shown in the following procedure and demo.
To generate a constructor using code completion:
Mail(String subject, String body) - generate, in this case). Notice that the IDE gives you the option of generating either the default constructor or a constructor which will initialize all uninitialized fields.
The IDE implements the selected constructor in the specified location. Note that the IDE also offers the possibility of overriding or implementing methods from superclasses and superinterfaces.
Generating a Constructor with Code Completion
The IDE's Java editor is capable of generating the constructs you commonly use automatically. In this section, we'll take a look at the procedure.
The next thing we want do is to generate the Getter and Setter
for the fields and make sure that we can use the class in collections,
which requires having
methods. The IDE's Java editor is capable of generating the constructs
you commonly use automatically. Notice as we go through the example
procedure that the IDE's Code Generation dialog also enables you to
do much more, including creating delegating methods, generating
constructors for intializing arbitrary set of fields, etcetera.
To generate getters and setters:
Getter and Setter..., in this case).
The IDE generates the getters and setters code automatically.
The next thing we need to do is to generate an Equals and Hashcode using Alt-Insert.
To generate equals and hashcode:
equals() and hashCode()...in this case).
The IDE generates the required equals and hashcode.
Generating Getters, Setters, Equals, and Hashcode
The IDE's Java editor provides several ways to add import statements for one or more classes, each offering you a list of possible classes to import when you click on the light bulb error mark or press the appropriate key combinations. In addition, the Editor also displays error hints that assist you with removing imports that aren't needed in your source.
After making the necessary changes to our file in the previous section, you may have noticed that the IDE's Error Stripe displayed a warning in the Editor's righthand margin indicating that there is an unused import in our file. In order to fix this, we'll use the IDE's helpful new Remove Unused Imports command so we can see how it works.
To remove unused imports:
The IDE removes the unused import statements from the file.
The IDE's Java editor also provides several other methods for managing imports, such as pressing Ctrl-Shift-I to import multiple types at once and pressing Alt-Shift-I while the cursor is positioned in a type identifier, which enables you to import that specific type.
The ErrorStripe and Removing Unused Imports
Here we want to create two private static fields of the string type for our username and password. Using the Editor's Line Tools feature, we can rapidly duplicate the line where the cursor is positioned and copy/paste or move it to a new line immediately below the first. This feature is particularly useful when repeatedly duplicating or moving lines for which most of the code is the same and only isolated portions need to be adjusted, such as in the case when initializing an array, for example. After doing so, you can then edit the field values as necessary.
To duplicate the current line or multiple selected lines:
The IDE duplicates the designated line or selection in the desired location.
To move current line or multiple selected lines:
The IDE moves the designated line or selection in the desired location.
Duplicating and Moving Single or Multiple Selected Lines
In this section we'll move to our project's Main.java file so we can write the code that will, as an example, check for new emails on the server while taking a second look a few of the commonly used features that we covered in the first part of the tutorial.
To further illustrate the simple process of using the new Editor's Code Completion, we first need to initialize our UI. Using Code Completion we can quickly accomplish this. We also need to place the value from the preferences into a string.
The NetBeans IDE 6.0 Editor in Action
In the next steps, we will add a class that will enable us to access the database to the Main.java file. While accomplishing this we'll take a look at a few more of the IDE Editor's helpful features.
In this section, we'll use Smart Code Completion to quickly add a method body to our file.
You may have noticed that the standard completion listbox is divided into two parts separated by a black line. The first section includes the smart completion items. To determine list of these items the code completion uses the current context (i.e. the position of caret) when completion is invoked. See other bullet points below starting with smart for more information and examples of the smart completion feature.
To add a method using Smart Code Completion:
Mail(String subject, String body) - generate, in this case) and press Enter.
The IDE implements the selected constructor in the specified location.
Smart Code Completion
The IDE's Live Templates feature enables you to enter entire snippets of code by simply typing the first few letter of the template's identifier. In this section, we'll use Live Templates to add a For Loop that iterates through the list of mail stored on the server.
To add an iterator using a Live Template:
The IDE adds the template code to the file at
the cursor position.
Note: Edit Template mode is maintained until you make an edit outside of the blue boxes (even if you move the cursor out of the blue editable boxes). If this occurs, use the Tab key to return to the template.
The Java editor's Highlights is an easy-to-use feature which you can think of as an enhanced alternate to editor Search. Based on the current cursor position, Code Highlighting renders all similar elements in the file with a background color so that you can see at-a-glance where they are located. In addition, the highlights are also shown in the error stripe, presenting a convenient overview of the whole file.
Notice that the IDE displays an error in the Annotation Stripe, telling us that the connect method throws an IOException which is not yet caught and we need to either catch it or add it into the throws clause of the enclosing method which can be done using a hint which after pressing Alt-Enter. Whenever you place the cursor in an exception, the Editor highlights all statements that throw the exception. If you place the cursor in a return type of a method, all exit points of the method will be highlighted. If you place the cursor in an element such as a field, for example, all usages of the field in the file are highlighted for easy identification and efficient navigation. Note also that the Editor places a marker in the Error Stripe indicating the location of the usage in the source.
To highlight all statements that throw an exception:
The IDE highlights all the places where that exception can be thrown.
If you place the cursor in the return type of the method definition, the Editor will highlight all exit points from the method (all expressions that throw the exception in each place they are returned).
To highlight method exit points:
The Editor highlights all expressions that throw the exception in each place they are returned. Notice that in this case, the closing bracket is highlighted because the execution can go through the end of the method.
Code Highlighting Variations
If you want to rename a private member (field, method, variable, etc.), you generally have several options. First, you could use Search and Replace, but you may end up replacing something you didn't want to replace if you're not careful. Second, you could use the Rename refactoring, however that would be a bit heavyweight for most scenarios. The third option, and the one we'll take a look at in this section, would be to use the Editor's new Instant Rename feature which you can use by simply placing the cursor on the identifier and pressing Ctrl+R.
To instantly rename:
The IDE updates all occurrences of the member in the file.
Instantly Renaming a Member