Source Editing in NetBeans IDE 6.0

This guide demonstrates the process of creating and editing Java sources using the new and improved NetBeans IDE 6.0 Editor.

Contents

  Content on this page applies to the NetBeans 6.0 IDE

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.

Software Needed for the Tutorial

Before you begin, you need to download or install the following software on your computer:

Using Code Completion and Code Generation

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.

Generating Code Using Code Completion

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:

  1. Place the cursor at the location in the file where you want to generate the constructor and press Ctrl+Space.
  2. In the Code Completion window that appears, select the constuctor which will initialize its fields (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.
  3. Press Enter.

    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


Generating Code Using the Code Generation Dialogs

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 equals() and hashCode() 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:

  1. Press Alt+Insert to invoke the code generation menu.
  2. Select the element that you want to generate from the list of displayed items (Getter and Setter..., in this case).
  3. In the dialog that appears, select the field for which you want to generate getters and setters and click OK.

    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:

  1. Press Alt+Insert to invoke the code generation menu.
  2. Select the element that you want to generate from the list of displayed items (equals() and hashCode()... in this case).
  3. In the dialog that appears, select the fields you want to base your equals and hashCode methods on and click OK.

    The IDE generates the required equals and hashcode.



Generating Getters, Setters, Equals, and Hashcode


Managing the Import Section

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:

  1. Place the cursor on the line of the class that wasn't imported and press Alt+Enter. Alternately, you can click the warning mark in the Annotation Stripe (along Editor's left edge).
  2. Choose Remove unused import from the pop-up menu. Notice that you can either remove one or all unused imports from the import section.

    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


Using Editor Line Tools

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:

  1. Position the cursor on the line or selection you want to duplicate.
  2. Press and hold Ctrl-Shift, then press either the Up or Down cursor key to copy and paste the code above or below the original line.

    The IDE duplicates the designated line or selection in the desired location.

To move current line or multiple selected lines:

  1. Position the cursor on the line or selection you want to move.
  2. Press and hold Alt-Shift, then press either the Up or Down cursor keys to move the current line or selection above or below the original line.

    The IDE moves the designated line or selection in the desired location.



Duplicating and Moving Single or Multiple Selected Lines



Smart Code Completion and Live Templates

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.

Smart Code Completion

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:

  1. Place the cursor in the location in the file where you want to generate the constructor and press Ctrl+Space.
  2. If you don't find the appropriate symbol, press Ctrl+Space a second time to display all symbols from the project class path, regardless whether imported or not.
  3. In the Code Completion window that appears, select the appropriate item (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


Live Templates

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:

  1. Type the first few letters of the template name and invoke the code completion by pressing Ctrl+Space.
  2. Choose the appropriate template. If there is a suitable collection it will be filled in for you with the editable parts of the template rendered in blue.
  3. Press Tab to cycle between the blue editable fields and update the template code as necessary.
  4. Press Enter or Esc to finish editing the 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.



Live Templates


Code Highlighting

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:

  1. Place the cursor on an exception in a method declaration throws clause.

    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:

  1. Place the cursor in the return type of the method definition.

    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


Instant Rename

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:

  1. Place the cursor on the identifier and press Ctrl+R. The IDE highlights all occurrences of the identifier in blue to indicate that Instant Rename mode is active.
  2. Edit the identifier as necessary to change all occurrences of the member.
  3. Press Enter or Esc to finish editing the identifier and exit Instant Rename mode.

    The IDE updates all occurrences of the member in the file.



Instantly Renaming a Member


top


Send Us Your Feedback

Next Steps

top