NetBeans source code editing tips: Smart code completion and dynamic templates

Source: Internet
Author: User
Tags exit mail netbeans

In this article, we'll look at the common features not mentioned in the first part of the tutorial by writing the sample code, which will check for new e-mail messages from the server.

To further illustrate the simple process of using the new editor's code completion, we first initialize the UI. We can implement it quickly with code completion. We also need to replace the preference value with a string.

Next, we'll add a class to the file that lets us access the database. In this process, we'll look at some of the more helpful features in the IDE editor.

Smart Code Completion

In this section, we'll use smart code completion to quickly add a method to our file.

You may notice that the standard Code completion box is divided into two parts by a black line. The first part contains the intelligent complement option. To list these options, the code completion exploits the context (for example, where the caret is inserted) when code completion is invoked. For more information and examples of smart code completion features, see the following section.

Add a method using smart code completion:

Place the cursor in the file where you want to build the constructor and press the Ctrl+space key.

If you cannot find the appropriate symbol, press the Ctrl+space key again to display all the symbols in the project classpath, whether imported or not imported.

In the displayed Code completion window, select the appropriate option (for example, Mail (string subject, String body)-build) and press the Enter key.

The IDE implements the selected builder in the specified place.

Dynamic Code Templates

The IDE's dynamic template features allow you to enter the entire snippet by simply entering the first few letters of the template identifier. In this section, we will use a dynamic template to add a for loop to traverse the mailing list stored on the server.

To add an iterator using a dynamic template:

Enter the first few letters of the code template and press the Ctrl+space key to enable code completion.

Select the appropriate template. If a suitable collection exists, the template automatically fills you with a blue editing area.

More Wonderful content: http://www.bianceng.cn

Press the TAB key to cycle through the blue Edit area and update the template code as needed.

Press Enter or the ESC key to end the edit template.

The IDE adds the template code where the cursor is located in the file.

Note: Edit Template mode continues until you have edited outside the blue box (even if you move the cursor outside the blue edit box). If this happens, use the Tab key to return to the template.

Code highlighting

The Java editor highlighting is an easy to use feature that you can think of as an enhancement to the editor lookup feature. Based on where the cursor is currently located, the code highlighting feature highlights all the similar elements in the file with a background color so you can see where they are. In addition, highlighted rows are displayed in the error bar to display an overview of the entire file.

Note that the IDE displays an error in the annotation bar, which tells us that the Connect method may throw an input/output exception (IOException) but is not captured. We have to capture it or add it to the throws clause of the method according to the prompts after pressing the Alt-enter key. As soon as you place the cursor over an exception, the editor highlights all statements that might throw the exception. If you place the cursor over the return type of a method, all exit points for the method are highlighted. If you place your cursor over an element, such as a field, all of the fields used in the file will be highlighted for easy identification and efficient navigation. Also note that the editor displays a tag in the error bar to show where it is used in the source file.

Highlight all statements that throw an exception:

Place the cursor on an exception to the throws clause of the method declaration.

The IDE highlights all the places where the exception might be thrown.

If you place the cursor over the return type of the method declaration, the editor highlights all return points returned from the method (where all the statements that throw the exception are returned).

Highlight the exit point for the method:

Place the cursor on the return type of the method declaration.

The editor highlights all expressions that throw an exception and where each explicit declaration returns. Note that the closing parenthesis of the method is highlighted when the return type is empty, because the execution is returned where the method ends.

Instant Rename

If you want to rename a private member (fields, methods, variables, and so on), you can usually have several choices. First, you can use Find and replace, but if you're not careful, you might replace the place you don't want to replace. Second, you can use refactoring to rename, but that's a bit overqualified. The third approach, which we will focus on in this section, can take the new feature of instant renaming in the editor by simply placing the cursor on the identifier you want to rename and pressing the Ctrl+r key.

Instant Rename:

Place the cursor over the identifier you want to rename and press the Ctrl+r key. The IDE highlights all occurrences of the identifier in blue highlighting so that the instant rename mode is activated.

Edit the identifier, where all occurrences of the member are changed.

Press Enter or ESC to finish editing the identifier and exit the instant rename mode.

The IDE updates all the places in the file where the member appears.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.