Code Assistance in the NetBeans IDE Java Editor: A Reference Guide

The purpose of any integrated development environment (IDE) is to maximize productivity and support seamless development from a single tool. This reference document describes useful code assistance features, customization options, and navigation capabilities of the NetBeans IDE's Java Editor.

This document applies to NetBeans IDE 7.1, 7.2, and 7.3 only. The version of this document applying to the most recent release of NetBeans IDE is here.


Content on this page applies to NetBeans IDE 6.9 and later

To complete this tutorial, you need the software and resources listed in the following table.

Software or Resource Version Required
NetBeans IDE version 6.9 or later
Java Development Kit (JDK) version 6 or 7

Smart Code Completion

The NetBeans IDE's Java Editor helps you quickly complete and generate code through the "smart" code completion feature. In a general sense, code completion is very useful when you want to fill in the missing code, look at the options available in the context of your application, and generate blocks of code when needed. See below for examples of how to use code completion.

Invoking Code Completion

Code completion box

Press Ctr-Space (or choose Source > Complete Code from the main menu) to open the code completion box. While you are typing, the list of suggestions shortens. The suggestions listed include those imported in your source file and symbols from the java.lang package.

To customize the code completion settings, select Tools > Options > Editor > Code Completion.

For example, you can set the code completion window to pop up either automatically or only on an as-needed basis. On the Code Completion tab, select the Auto Popup Completion Window checkbox to invoke the code completion window automatically when you are typing certain characters. The default character is ".", but you can add your own characters.

To add characters that invoke the code completion window, select Java from the Language drop-down list and type your characters in the Auto Popup Triggers for Java field. The code completion window will pop up every time you type the specified characters.

When the Auto Popup Completion Window checkbox is disabled, you need to press Ctr-Space each time you want to use code completion.

Smart Suggestions at the Top

Code completion box with smart suggestions

In the NetBeans IDE, Java code completion is "smart," which means that the suggestions that are the most relevant for the context of your code are displayed at the top, above the black line in the code completion window.

In the example on the left, the editor suggests inserting the LinkedHashMap constructor from the java.util package.

If the "smart" suggestions are not the ones you want to use, press Ctr-Space again to see a complete list.

Completing Keywords

Completion of keywords

Use code completion (Ctr-Space) to complete keywords in your code. The editor analyzes the context and suggests the most relevant keywords.

In the example on the left, the ColorChooserDemo class extends the JPanel class. You can quickly add the keyword extends from the suggested items.

Suggesting Names for Variable and Fields

Completion of names and fields

When you are adding a new field or a variable, use code completion (Ctr-Space) to choose a name that matches its type.

Type a prefix for the new name, press Ctr-Space and select the name you want to use from the list of suggestions.

Suggesting Parameters

Suggestions for parameter formats

The editor guesses on the parameters for variables, methods, or fields and displays the suggestions in a pop-up box.

For example, when you select a method from the code completion window which has one or more arguments, the Editor highlights the first argument and displays a tooltip suggesting the format for this argument. To move to the next argument, press the Tab or Enter keys.

You can invoke the tooltips with method parameters by pressing Ctr-P (or Source > Show Method Parameters) at any time.

Common Prefix Completion

Completion of prefixes

You can use the Tab key to quickly fill in the most commonly used prefixes and single suggestions.

To check out how this feature works, try typing the following:

  1. Type System.out.p and wait for code completion to show all fields and methods that start with "p." All the suggestions will be related to "print."
  2. Press the Tab key and the editor automatically fills in the "print".
    You can continue and type "l" and, after pressing Tab, the "println" will be added.

In the code completion window, icons are used to distinguish different members of the Java language. See Appendix A at the end of this document to see the meanings of these icons.


Managing Imports

There are several ways of how you can work with import statements. The IDE's Java Editor constantly checks your code for the correct use of import statements and immediately warns you when non-imported classes or unused import statements are detected.

Suggestions for an import statement


When a non-imported class is found, the Bulb error icon error mark appears in the IDE's lefthand margin (this margin is also called the glyph margin). Click the error mark and choose whether to add the missing import or create this class in the current package.

While you are typing, press Ctrl-Shift-I (or choose Source > Fix Imports from the menu) to add all missing import statements at once.

Press Alt-Shift-I to add an import only for the type at which the cursor is located.


Automatic addition of import statements

When you select a class from the code completion window, the Editor automatically adds an import statement for it, so you do not need to worry about this.

Automatic removal of unused imports

If there are unused import statements in your code, press the Bulb warning icon warning mark in the Editor lefthand margin and choose either to remove one unused import or all unused imports. In the Editor, unused imports are underlined (see the Semantic Coloring section for details).

To quickly see if your code contains unused or missing imports, watch the error stripes in the righthand margin: orange stripes mark missing or unused imports.


Generating Code

When working in the Java Editor, you can generate pieces of code in one of the two ways: by using code completion or from the Code Generation dialog box. Let's take a closer look at simple examples of automatic code generation.

Using the Code Generation Dialog Box

Code generation from the Code Generation dialog box


Press Alt-Insert (or choose Source > Insert Code) anywhere in the Editor to insert a construct from the Code Generation box. The suggested list is adjusted to the current context.

In the example on the left, we are going to generate a constructor for the Sample class. Press Alt-Insert, select Constructor from the Code Generation box, and specify the fields that will be initialized by the constructor. The Editor will generate the constructor with the specified parameters.

In the IDE's Java Editor, you can automatically generate various constructs and whole methods, override and delegate methods, add properties and more.

Using Code Completion

Code generation from the Code Completion window

You can also generate code from the code completion window. In this example, we use the same piece of code as above to show how you can generate code from the code completion window.

Press Ctrl-Space to open the code completion window and choose the following item: Sample(String name, int number) - generate. The Editor generates a constructor with the specified parameters.

In the code completion window, the constructors that can be automatically generated are marked with the Generate icon icon and the "generate" note. For more explanations of the icons and their meanings, see Appendix A.


Code Templates

A Code Template is a predefined piece of code that has an abbreviation associated with it. See the examples below that show how you can use code templates.

Using Code Templates

Insertion of a template from the Code Completion window

Code templates are marked with the Live template icon icon in the code completion window.

You can do one of the following:

  • Select a template from the code completion window and press Enter or
  • Type the abbreviation for this template and press the key that expands this template (by default, Tab).

In the expanded template, editable parts are displayed as blue boxes. Use the Tab key to go through the parts that you need to edit.

Adding or Editing Code Templates

Customizing live templates

To customize Code Templates:

  1. Choose Tools > Options > Editor > Code Templates.
  2. From the Language drop down list, select Java (or whichever language you want to create a code template for). The list of abbreviations and associated templates is displayed.
  3. Use the New and Remove buttons to add or remove templates in the list. To edit an existing template, select the template and edit the code in the Expanded Text field below the list.
  4. Choose the key which will be used to expand the templates. The default key is Tab.

See this document to know more about the syntax for writing new Code Templates.

See also Code Templates in NetBeansIDE for PHP.


Working with Javadoc

Use the following features that facilitate working with Javadoc for your code.

Displaying Javadoc

Javadoc window.

Place the cursor on an element and press Ctr-Shift-Space (or choose Source > Show Documentation). The Javadoc for this element is displayed in a popup window.

In the IDE's main menu, click Window > Other > Javadoc to open the Javadoc window, in which the documentation is refreshed automatically for the location of your cursor.




Creating Javadoc Stubs

Creation of Javadoc stubs

Place the cursor above a method or a class that has no Javadoc, type "/**", and press Enter.

The IDE creates a skeletal structure for a Javadoc comment filled with some content. If you have a Javadoc window open, you will see the changes immediately while you are typing.


Using Javadoc Hints

Use of Javadoc hints

The IDE displays hints when Javadoc is missing or Javadoc tags are needed.

Click the bulb icon on the lefthand margin of the editor to fix Javadoc errors.

If you do not want to see the hints related to Javadoc, choose Tools > Options > Editor > Hints, and clear the Javadoc checkbox in the list of hints that are displayed.

Using Code Completion for Javadoc Tags

Code completion for Javadoc

Code completion is available for Javadoc tags.

Type the "@" symbol and wait until the code completion window opens (depending on your settings, you may need to press Ctr-Space).

Generating Javadoc

Generated Javadoc output

To generate Javadoc for a project, choose Run > Generate Javadoc menu item (or right-click the project in the Projects window and choose Generate Javadoc). The IDE will generate the Javadoc and open it in a separate browser window.

In the example on the left, you can see a sample output of the Generate Javadoc command. If there are some warnings or errors, they are also displayed in this window.

To customize Javadoc formatting options, right-click the project, choose Properties and open the Documenting panel under the Build category (available on Java projects only). For information about the options on this panel, click the Help button in this window.

Analyzing Javadoc

Javadoc Analyzer window

To identify the places in your code that need Javadoc comments and quickly insert these comments, you can use the Javadoc Analyzer tool available in the Java Editor.

To analyze and fix Javadoc comments:

  1. Select a project, a package, or an individual file and choose Tools > Analyze Javadoc from the main menu.
    The Analyzer window displays suggestions for adding or fixing Javadoc comments, depending on the scope of your selection.
  2. Select one or several checkboxes where you would like to fix Javadoc and click the Fix Selected button.
  3. Click Go Over Fixed Problems and use the Up and Down arrows to actually add your comments. This might be helpful if you selected to fix several instances at once and now want to revisit the stubs.


Using Hints

While you are typing, the Java Editor checks your code and provides suggestions of how you can fix errors and navigate through code. The examples below show the types of hints that are available in the Editor and how to customize them.

Using Hints to Fix Code

Using hints for fixing code

For the most common coding mistakes, you can see hints in the lefthand margin of the Editor. The hints are shown for many types of errors, such as missing field and variable definitions, problems with imports, braces, and other. Click the hint icon and select the fix to add.

Hints are displayed automatically by default. However, if you want to view all hints, choose Source > Fix Code (or press Alt-Enter).

For example, try typing "myBoolean=true". The editor detects that this valiable is not defined. Click the hint icon and see that the Editor suggests that you create a field, a method parameter, or a local variable. Select

Customizing Hints

Customizing options for hints

You might want to limit the number of categories for which hints are displayed. To do this:

  1. Choose Tools > Options > Editor > Hints.
  2. From the Language drop-down list, select Java and view a list of elements for which hints are displayed (their checkboxes are selected).
  3. To disable hints for some categories, clear the appropriate checkboxes.

Note: On the Hints tab, you can also disable or limit the scope of dependency scans (Dependency Scanning option). These steps can singificantly improve the performance of the IDE.

The IDE detects compilation errors in your Java sources by locating and recompiling classes that depend on the file that you are modifying (even if these dependencies are in the files that are not opened in the editor). When a compilation error is found, red badges are added to source file, package, or project nodes. Dependency scanning within projects can be resource consuming and degrade performance, especially if you are working with large projects.

To improve IDE's performance, you can do one of the following:

  • Limit the scope of dependency scans to the Source Root (search for dependencies only in the source root where the modified class is located) or current Project.
  • Disable dependency scanning (choose Project Properties > Build > Compiling and deselect the Track Java Dependencies option). In this case, the IDE does not scan for dependencies or updates the error badges when you modify a file.

Surround With...

Surround with options

You can easily surround pieces of your code with various statements, such as for, while, if, try/catch, and other.

Select a block in your code that you want to surround with a statement and click the bulb icon in the lefthand margin (or press Alt-Enter). The editor displays a list of suggestions from which you select the statement you need.

General Editor Features

Code Formatting

Formatting customization options

Choose Source > Format or press Alt-Shift-F to format the entire file or a selection of code. The IDE formats the code in accordance with the specified formatting settings.

To customize the formatting settings for Java code:

  1. Choose Tools > Options > Editor > Formatting.
  2. From the Language drop-down list, select Java.
  3. From the Category drop-down list, select the category that you would like to customize. For example, you can customize the number of blank lines, the size of tabs and indentation, wrapping style, etc.
  4. Modify the rules for the selected category and preview the result.

Inserting and Highlighting Braces, Brackets, and Quotes

Customization options for brackets

By default, the IDE automatically inserts matching pairs of braces, brackets, and quotes. When you type an opening curly brace and then press Enter, the closing brace is added automatically. For (, [, ", and ', the editor inserts a matching pair right away.

If, for some reason, this feature is disabled, enable it as follows:

  1. Choose Tools > Options > Editor > Code Completion.
  2. Select the Insert Closing Brackets Automatically checkbox.

The editor also highlights matching pairs of braces, brackets and quotes. For example, place the cursor before any brace or bracket and, if it has a matching pair, both will be highlighted in yellow. Single brackets of any type are highlighted in red and the error mark is displayed in the lefthand margin.

To customize the highlight colors, choose Tools > Options > Fonts & Colors > Highlighting.

Code Folding

Code folding example

In the Java Editor, you can quickly collapse and expand blocks of code, such as method declaration, Javadoc comments, import statements, etc. Collapsable blocks are shown with gray lines and plus/minus signs near the lefthand margin of the editor.

  • The easiest way to collapse a block of code is to click the gray lines with a minus character in the lefthand margin.
  • To fold all collapsable blocks in a file, right-click in the editor and choose Code Folds > Collapse All from the pop-up menu.
  • From the Code Folds > Collapse All pop-up menu, you can choose to collapse all Javadoc comments or all Java code in a file.
  • You can mouse over the folded elements to quickly review the hidden parts.

To customize the code folding options:

  1. Choose Tools > Options > Editor > General.
  2. To disable code folding, clear Use Code Folding. Note that code folding is enabled by default.
  3. Select the blocks of code to be collapsed by default when you open a file.

Customizing Keyboard Shortcuts

Keyboard customization options

In the NetBeans IDE, choose Tools > Options > Keymap to customize keyboard shortcuts. You can do this in several ways:

  • Select a predefined set of keyboard shortcuts, which is called Profile.
  • Edit particular keyboard shortcuts.

You can save customized sets of your shortcuts as profiles. Then, you can switch from one profile to another to quickly change multiple settings. For example, to create a custom profile of keyboard shortcuts:

  1. In the Options > Keymap window, click Manage profiles.
  2. Select the profile you want to use as a base for your new profile and click Duplicate.
  3. Enter the new profile name and click OK.
  4. Ensure that the new profile is selected and modify the shortcuts you need.
    To edit a shortcut, double-click in the Shortcut field or click the ellipsis button (...). As you press the sequence of keys, the syntax for them is added.
    If you want to add special characters, such as Tab, Escape, or Enter, click the ellipsis button (...) again and select the key from the pop-up window.
  5. When finished editing, click OK in the Options window.

To find a shortcut for a specific command, type the command name in the Search field. To find a command by a combination, insert the cursor in the Search in Shortcuts field and press the shortcut key combination.


Semantic Coloring and Highlighting

The IDE's Java Editor shows code elements in distinct colors, based on the semantics of your code. With semantic coloring, it becomes easier for you to identify various elements in your code. In addition to coloring, the Java Editor highlights similar elements with a particular background color. Thus, you can think of the highlighting feature as an alternative to the Search command, because in combination with error stripes, it gives you a quick overview of where the highlighted places are located within a file.

Customizing Colors

Customization options for coloring

To customize semantic coloring settings for the Java Editor, choose Tools > Options > Fonts and Colors.

The IDE provides several preset coloring schemes, which are called profiles. You can create new profiles with custom colors and quickly switch between them.

It is very convenient to save custom colors in new profiles. For example, do the following:

  1. In the Options > Fonts and Colors window, click Duplicate next to the Profile drop-down list.
  2. Enter the new profile name and click OK.
  3. Ensure that the new profile is currently selected and choose Java from the Language drop-down list.
  4. Select a category and change the font, font color (Foreground), background color, and effects for this category.
    Use the Preview window to view the results.
  5. Click OK.

Note: All NetBeans IDE settings and profiles are stored in the NetBeans userdir (refer to the FAQ on how to locate the userdir for your operating system). When upgrading to newer versions of NetBeans, you can export old settings and import them to the newer version.

To export the IDE settings:

  1. In the Options window (Tools > Options), click Export.
  2. Specify the location and name of the ZIP file that will be created.
  3. Select the settings that you want to export and click OK.

To import the IDE settings:

  1. In the Options window (Tools > Options), click Import.
  2. Specify the ZIP file with IDE settings or path to the userdir from a previous version.
  3. Select the settings that you want to import and click OK.


Coloring Example

Image showing a coloring example

In the left, you can see an example of a coloring scheme. Depending on your custom settings, your colors might look differently than those shown in the screenshot.

Distinct colors are used for keywords (blue), variables and fields (green), and parameters (orange).

References to deprecated methods or classes are shown as strikethrough. This warns you when you are going to write code that relies on deprecated members.

Unused members are underlined with a gray wavy line. Comments are displayed in gray.


Using Highlights

Image showing the use of highlights

The IDE highlights usages of the same element, matching braces, method exit points, and exception throwing points.

If you place the cursor in an element, such as a field or a variable, all usages of this element are highlighted. Note that error stripes in the Editor's righthand margin indicate the usages of this element in the entire source file (see Error Stripes). Click the error stripe to quickly navigate to the desired usage location.

If you decide to rename all the highlighted instances, use the Instant Rename command (Ctrl-R or choose Refactor > Rename).



The Java Editor provides numerous ways of how you can navigate through code. See below for several examples that show the navigation features of the Java Editor.

Error Stripes

Error stripes in the righthand margin of the editor provide a quick overview of all marked places in the current file: errors, warnings, hints, highlighted occurences, and annotations. Note that the error stripe margin represents an entire file, not just the part that is currently displayed in the editor. By using error stripes, you can immediately identify whether your file has any errors or warnings, without scrolling through the file.

Click an error stripe to jump to the line that the mark refers to.

Navigating From the Editor: Go To...

Image showing the Go To command

Use the following the "Go To.." commands located under the Navigate menu item to quickly jump to target locations:

  • Go To Declaration (Ctr-B, by default). Hold down the Ctrl key and click the usage of a class, method, or field to jump to its declaration. You can also place the cursor on the member (a class, method, or field) and choose Navigate > Go To Declaration or right-click and choose Navigate > Go To Declaration from the pop-up menu.
  • Go To Source (Ctrl-Shift-B, by default). Hold down the Ctrl key and click a class, method, or field to jump to the source code, if the source is available. You can also place the cursor on the member (a class, method, or field) and either press Ctrl-Shift-B or choose Navigate > Go To Source in the main menu.

Image showing the Go To Type dialog box

  • Go To Type (Ctrl-O), Go To File (Alt-Shift-O), and Go To Symbol (Ctrl-Alt-Shift-O). If you know the name of the type (class, interface, annotation or enum), file, or symbol to where you want to jump, use these commands and type the name in the new window. Notice that you can use prefixes, camel case, and wildcards.

Image showing the Go To Line dialog box

  • Go To Line (Ctrl-G). Enter the line number to which you want to jump.

Jumping to Last Edit

Image showing the Jumping to Last Edit button

To quickly return to your last edit, even if it is in another file or project, press Ctrl-Q or use the button in the top left corner of the Java Editor toolbar. The last edited document opens, and the cursor is at the position, which you edited last.

Switching Between Files

Image showing the buttons used to switch between open files


There are two very handy features that allow you to switch between open files:

  • Go Back (Alt-Left) and Go Forward (Alt-Right). To go to the previously edited file or move forward, choose Navigate < Back or Navigate < Forward or press the corresponding buttons on the editor toolbar (shown in the figure). The file opens and the cursor is placed at the location of your last edit. When you click one of these buttons, you can expand the list of the recent files and click to navigate to any of them.

Image showing a pop-up menu for toggling between files

  • Toggle Between Files (Ctrl-Tab). After you press Ctrl-Tab, all open files are shown in a pop-up window. Hold down the Ctrl key and press several times the Tab key to choose the file you would like to open.

Using Bookmarks

Image showing an example of a bookmark

You can use bookmarks to quickly navigate through certain places in your code.

Press Ctrl-Shift-M (or right-click the left margin and choose Bookmark > Toggle Bookmark) to bookmark the current line. The bookmarked line is shown with a small blue icon in the left margin (see the figure).

To remove the bookmark, press Ctrl-Shift-M again.

To go to the next bookmark, press Ctrl-Shift-Period, to go to the previous bookmark, press Ctrl-Shift-Comma.

Using the Navigator

Example of the Navigator window

The Navigator window provides structured views of the file you are working with and lets you quickly navigate between different parts of the file.

To open the Navigator window, choose Window > Navigating > Navigator or press Ctrl-7.

In the Navigator window, you can do the following:

  • Choose between different views: Members, Bean Patterns, Trees, Elements, etc.
  • Double-click an element to jump to the line where it is defined.
  • Right-click an element and apply commands, such as Go to Source, Find Usages, and Refactor.
  • Apply filters to the elements displayed in the Navigator (use the buttons at the bottom).
  • Type the name of the element that you want to find (the Navigator window must be active).


See Also


Appendix A: Icons in the Code Completion Window

Icon Meaning Variants (if any)


Annotation type icon Annotation type    
Class icon Class    
Package icon Package    
Enum type icon Enum type    
Code Template icon Code Template    
Constructor icon Constructor New constructor icon New constructor (generate)
    Protected constructor icon Protected constructor
    Private constructor icon Private constructor
    Package private constructor icon Package private constructor
Field icon Field Protected field icon Protected field
    Private field icon Private field
    Icon for package private field Package private field
Icon for static fields Static field Icon for protected static fields Protected static field
    Icon for private static fields Private static field
    Icon for package private static fields Package private static field
Interface icon Interface    
Java keyword icon Java keyword    
Method icon Method Icon for protected methods Protected method
    Icon for private methods Private method
    Icon for package private methods Package private method
Icon for static methods Static method Icon for protected static methods Protected static method
    Icon for private static methods Private static method
    Icon for package private static methods Package private static method
Local variable icon Local variable    
Attribute icon Attribute    
get support for the NetBeans