Java EditorUsersGuide

Revision as of 23:40, 14 October 2010 by Jeff rubinoff (Talk | contribs)

NetBeans 5.5 / 6.0 Editor User's guide (AKA Tips & Tricks) (AKA Demo script)

While many of these features are new to NetBeans version 6.0, some were present in previous versions. Note that some of the default keyboard shortcuts for those pre-existing features have changed. For example, the fix-imports shortcut changed from Alt+Shift+F to Ctrl+Shift+I.

Utilize the menu item 'Help -> Keyboards Shortcut Card' for your current version of NetBeans. Alternatively, if you want to keep using the version 5.5 keyboard shortcuts in NetBeans 6.0+, choose 'Tools -> Options', click the 'Keymap' group and set the 'Profile:' (near the top) to "NetBeans 5.5".

See also: [NetBeans IDE 6.8 Editor]


How to use Semantic Coloring

Semantic coloring lets the IDE show distinct colors, based on the code's semantic information. In this way, you can immediately recognize what you are doing in the editor.

  • Fields, parameters, variables: This is probably the most important coloring the editor offers. You can immediately see whether you are working with a field, parameter or local variable. This may help you recognize whether you are modifying the a class's state or whether your method has no side effect.


  • Static members: Static members are distinguished from instance members (by using an italic font by default). If you see a collection in italics you should think twice before you put something into it; it may result in a nice memory leak.


  • Deprecated members: References or calls to deprecated methods or classes are rendered in strike-through text. This should warn you immediately when you write code relying on deprecated code.


  • Unused members: Unused private methods, classes, variables and fields are drawn in grey (by default). You may want to get rid of such declarations.


  • Unused imports: Unused imports are drawn in grey (again, by default). They also produce warnings and let you fix them using a hint (by either clicking the light bulb or pressing {Alt+Enter}.


  • Javadoc: HTML code in Javadoc is now syntactically colored for better readability.


  • Other colorings: For a complete list of semantic colorings, go to Tools->Options->Fonts & Colors and pick Java from the "language" combo box. Please let us know if we are missing your favourite semantic coloring.


Demo class name: AllColorings

How to use Code Completion

Code completion has been improved in several ways.

  • Code completion behavior Ctrl+Space lists symbols which are already imported in your source file, plus of course symbols from the java.lang package. Pressing Ctrl+Space again will list all symbols from the project class path whether imported or not. If you want to go to the list of all types immediately you can use the All Symbols Completion, which is invoked by pressing Ctrl+Alt+Space.

  • Smart completion - Notice that the standard completion listbox is divided into two parts separated by a black line. The first section includes smart completion items. The editor chooses these items based on the current context (i.e. around the current caret position) when completion is invoked. See other bullet points below starting with "smart" for more information and examples of this smart completion feature.

  • Completing Keywords You may use code completion for completing keywords: the editor knows which keywords fit into which place in your code. Just don't be surprised if you don't see the keyword if. It is so short that we decided not to put it into code completion.


  • Field/Variable names When you introduce a new field or variable, you often want its name to mimic its type. Code completion now supports this. To prepend a prefix to the guessed name, simply type the prefix and then invoke code completion. This works for method names as well.


  • Smart completion examples Here is a list of places where smart completion kicks in.

  • After new (with generics) This will help especially when you are declaring a field or variable with complicated generics.


  • In method call parameters Smart completion will offer only those fields, variables, methods which can be passed as an argument to the method.


  • Smart code completion for types

  • For exceptions In a catch block smart completion will offer only those exceptions which have been thrown in the try block but not yet caught.


  • Parameter guessing Code completion of method calls guesses which variable, field or parameter should be used based on type and similarity of the name. See the screenshot to see how the correct variables were picked by code completion when completing the call to the createRectangle() method.


  • Code completion ending character Notice that you may select the item of choice with different keys. Selecting with Enter you'll get what you expect. Should you select the item with ".", "," or ";" code completion will append the character you typed at the end of the completed word.

  • Generics in generation of implementation

  • Creation of elements - Invoking code completion between class members will offer to create a constructor (either a default constructor or one initializing uninitialized fields), override methods or implementation methods. For more code generation features please look at the code generation dialog section.


  • Completion for annotations

  • Completion in Collections.<String>| - works fine when Collections is imported.

How to use Code Generation dialog

  • The Java editor is capable of generating often used constructs for you automatically. Press Alt+Insert to invoke the code generation menu and pick what you want to generate.


How to add import statements for one or more classes

There are several ways to add import statements.

  • Use error hints: Click on the error mark with the light bulb or press Alt+Enter. It will offer you a list of possible classes to import. Pick the proper hint and you are done.


  • Use the import class dialog: Put the caret into the name of an unimported class and press Alt+Shift+I. A list of possible classes to import will appear.


  • Use the smart fix import: Press Ctrl+Shift+I. You will get a dialog that lists all unresolved identifiers in the source. If there is more than one option for resolving the identifier you may choose using the combo box. Classes shown in gray do not fit for some reason. In the example below the Toolkit class in the package does not have the method getDefaultToolkit(), which is called in the source.


How to get rid of unused imports

Put the caret on a line with an unused import (you may do so by clicking the warning mark in the error stripe) and invoke hints. You invoke hints either by using the mouse or pressing Alt+Enter.


How to use Highlights

You can think of the highlights feature as an easy-to-use and more correct substitution for the editor's Search. The IDE tracks the position of the caret and, based on it, highlights some parts of the code. The highlights are marked with a background color in the editor. They are also put into the error stripe, which gives you an overview of the whole file. See "How to use instant rename" to learn about a similar substitution in the Search and Replace feature.

  • Usages of element: Putting the caret on an identifier will highlight all usages of that identifier in the current source code.


  • Method exit points: Putting the caret on the return type of a method definition will highlight all places where the method can exit.


  • Exception throwing points: Putting the caret on an exception in a method declaration throws clause will highlight all the places where that exception can be thrown.


  • Other highlights Putting the caret on an identifier in the extends or implements clause of a class will highlight all methods which override or implement methods in given class or interface. There are more such highlights. For complete list see the Mark Occurrences options.

TBD Mark Occurences options dialog screenshot when implemented

Demo class name: Main

How to use Instant Rename

  • If you want to rename a private member (field, method, variable ...), you have several options. a) Use the editor's "Search and replace", where you may end up replacing something you did not want to replace. b) Use the "Rename" refactoring, which is a bit heavyweight. Or you can use the instant rename feature.
    Simply put the caret on the identifier and press Ctrl+R. All occurrences of the identifier will be highlighted in blue, and when you start editing they will all change. Press Esc or Enter to finish the instant rename.


How to use Surround With

  • If you want to surround a piece of text with a template, just select the text and invoke hints with Alt+Enter.


How to use Java syntax-based selection

  • Sometimes it is useful to select text based on Java syntax. To do this, press Alt+Shift+PERIOD (Ctrl-Shift-PERIOD on a Mac) to expand the selection and Alt+Shift+COMMA (Ctrl-Shift-COMMA on a Mac) to shrink the selection based on the structure of your Java source code.


How to use the Error Stripe

  • The editor uses the error stripe on the right side of it to show information about interesting places in the code. These places include, for example:
  • The current line
  • Errors
  • Warnings
  • Highlights (like usages of members or method exit points)
  • Places where the code has been changed
  • ...

The places are represented by small stripes of different colors.

How to use Live Templates

Type a few letters from the name of the template e.g. fo| and invoke the code completion.


Pick the template you want to use. If there is a suitable collection it will be filled in for you. Several parts of the template will be rendered in blue. You may cycle between them using the Tab key. Editing the fields will change the part of the code accordingly. Press Enter or Esc to finish editing the template. Notice that if you move out of the blue boxes with the cursor the "edit template mode" continues (so you may get back using the Tab key) until you actually change text outside of the blue box.


  • Writing a new template You can define new templates: go to Menu->Tools->Options, then choose the Editor category and the Code Templates tab. You may want to look at the existing templates and try to create your own. Or see below for a concise description of the template language.


Any code template parameter can be specified by its name and a set of optional hints. Hints guide the infrastructure in computing the values assigned to parameters on template expansion. The parameter definition syntax is ${param_name hint=value hint=value ...}

As an exception, boolean hints can be written without the value part: ${param_name hint} translates to ${param_name hint=true}.

The following parameter names are reserved by the infrastructure for handling the caret and selection position:

${cursor} defines a position where the caret will be located after the editing of the code template values finishes.
${selection} defines a position for pasting the content of the editor selection. This is used by so-called 'selection templates' that appear as hints whenever the user selects some text in the editor.

Similarly, some hint names are reserved by the code template infrastructure: General:

${param_name default="value"} defines the parameter's default value.
${param_name editable=false} can be used to disable the user's editing of the parameter.

Language specific:

${param_name instanceof="java.util.Collection"} Requires the parameter value to be an instance of the given type Java? PHP?
${param_name array} requires the parameter value to be of an array type (including arrays of primitive data types). Y N
${param_name iterable} requires the parameter value to be of an array type or an instance of "java.lang.Iterable". Can be used in 'for-each' cycles. Y N
${param type="java.util.Iterator"} requires the parameter value to be the given type. The infrastructure will try to use short name Iterator and import java.util.Iterator if possible.
${param_name iterableElementType} requires the parameter value to be the type of the iterable element. Can be used in 'for-each' cycles. Y N
${param_name leftSideType} requires the parameter value to be the type of the expression on the assignment's left side. Y N
${param_name rightSideType} requires the parameter value to be the type of the expression on the assignment's right side. Y N
${param_name cast} defines that the parameter value would be a type cast if necessary. Y N
${param_name newVarName } defines that the parameter value should be a 'fresh' unused variable name in the given context. Y Y

As an example, consider the definition of the "iterator type" parameter of the forc standard template: ${IT_TYPE rightSideType type="java.util.Iterator" default="Iterator" editable=false} where:

  • IT_TYPE is the parameter name,
  • rightSideType tells the infrastructure to try to resolve the type of an expression on the right side of an assignment (e.g. java.util.Iterator<java.lang.String>) and use it as the parameter value (with possible autoimporting).
  • If the type cannot be resolved, type="java.util.Iterator" tells the infrastructure to try to resolve the java.util.Iterator type and use it as the parameter value (with possible autoimporting).
  • If the type cannot be resolved, default="Iterator" tells the infrastructure - use string "Iterator" as the parameter value.
  • editable=false tells the infrastructure not to allow a user to change the parameter value.
class name: LiveTemplates

How to get to Javadoc quickly and how to fix Javadoc using hints

  • Javadoc popup window Put the caret on a symbol of choice and press Ctrl+Shift+Space.


  • Javadoc view window Go to Window->Javadoc View in the main menu. This will open a separate window which always shows Javadoc for the element under the caret.


  • Hints for fixing Javadoc. Missing or erroneous Javadoc can now be discovered and fixed using hints. As there is no GUI yet for hints settings you might be interested in the following system properties which can limit the number of hints shown.


How to navigate through code

  • Opening a type in the editor If you know the name of the type (class, interface, annotation or enum) you want to open, press Alt+Shift+O and type the name into the dialog. Notice that you can use prefix, camel case and wild cards.


  • Navigating to source or declaration Place the caret on a type or variable name and press Alt+O or Alt+G. The former will open the source declaration of the type in the editor; the latter will take you to the declaration (e.g. to the place where a variable is defined.)


  • Hyperlinking A similar action can be achieved by holding down the Ctrl key and clicking on an element with the mouse.


  • Implements/Overrides annotation Methods which implement or override another method are marked with a glyph in the editor gutter. Clicking the icon will take you to the overridden method or to the method from the interface. Pressing Ctrl+B does the same.


  • Declaration Window If you want to see the declaration of a given element but you don't want to open it in the editor, you can open the Declaration View, which always shows the declaration of the element under the caret.


  • Navigator window This window (which can be opened from the Window menu) always shows the members of the class opened in the editor or selected in the explorer. Double clicking on an item will take you to its source in the editor. If you start typing in the window you can easily find an element by its name.


  • Outlines Will show either the members or the inheritance hierarchy of the element open in the editor (when invoked from the main menu) or currently under the caret (when invoked from the editor popup menu).



Image:NAV_HierarchyOutline_Java_EditorUsersGuide.jpg }}

How to Refactor code

Place the caret on the symbol you want to refactor. Pick the refactoring either from the main menu or from the editor popup menu.

Image:RF_Menu_Java_EditorUsersGuide.png }}

Fill in the necessary data in the refactoring dialog and choose either to refactor directly or show a preview of the refactoring.

Image:RF_Dialog_Java_EditorUsersGuide.png }}

In the refactoring preview you can look at the diffs to make sure that it will make the desired change to your code.


Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo