Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Using Code Completion

When you are typing Java identifiers in the Source Editor, you can use the IDE's code completion box to help you finish expressions, as shown in Figure 5-1. In addition, a box with Javadoc documentation appears (as shown in Figure 5-2) and displays documentation for the currently selected item in the code completion box.

keegan_fig05_02.jpg

Figure 5-2 Javadoc box that accompanies the code completion box

Beginning with NetBeans IDE 5.0, many types of code generation have been added to the code completion box. Using the code completion box, you can:

  • Fill in names of classes and class members. (After you select a class to fill in, an import statement is also filled in, if appropriate.)
  • Browse Javadoc documentation of available classes.
  • Generate whole snippets of code from dynamic code templates. You can customize code templates and create new ones. See Inserting Snippets from Code Templates for more information.
  • Generate getter and setter methods.
  • Generate skeletons for abstract methods of classes extended by and interfaces implemented by the current class.
  • Override inherited methods.
  • Generate skeletons of anonymous inner classes.

To open the code completion box, do one of the following:

  • Type the first few characters of an expression and then press Ctrl-spacebar (or Ctrl-\).
  • Pause after typing a period (.) in an expression.
  • Type a space, and then pause for a moment.

The code completion box opens with a selection of possible matches for what you have typed so far.

To narrow the selection in the code completion box, continue typing the expression.

To complete the expression and close the code completion box, do one of the following:

  • Continue typing until there is only one option left and then press Enter.
  • Scroll through the list, using the arrow keys or your mouse to select a value, and then press Enter.

To close the code completion box without entering any selection, press Esc.

To complete the expression and leave the code completion box open, select a completion and press the period (.) key. This is useful if you are chaining methods. For example, if you want to type

 getRootPane().setDefaultButton(defaultButtonName)

you might do the following:

  1. Type getRo (which would leave only getRootPane() in the code completion box) and press the period (.) key.
  2. Type .setDef (which should make setDefaultButton(JButton defaultBut-ton) the selected method in the code completion box, as shown in Figure 5-3) and press Enter. getRootPane().setDefaultButton() should now be inserted in your code with the insertion point placed between the final parentheses. A tooltip appears with information on the type of parameter for you to enter.
    keegan_fig05_03.jpg

    Figure 5-3 Code completion box with setDefaultButton (JButton defaultButton) selected

  3. Type a name for the parameter.
  4. Type a semicolon (;) to finish the statement. The semicolon is automatically placed after the final parenthesis.

Code Completion Tricks

When typing with the code completion box open, there are a few tricks you can use to more quickly narrow the selection and generate the code you are looking for. For example:

  • You can use "camel case" when typing class names. For example, if you want to create an instance of HashSet, you can type private HS and press Ctrlspacebar to display HashSet (and other classes that have a capital H and a capital S in their names).
  • You can use the comma (,) and semicolon (;) keys to insert the highlighted item from the code completion box into your code. The comma or semicolon is inserted into your code after the chosen item from the code completion box.
  • You can fill in text that is common to all of the remaining choices in the list by pressing Tab. This can save you several keystrokes (or use of the arrow keys or mouse) when the selection in the code completion box is narrowed to choices with the same prefix. For example, if you are working with a Hashtable object ht, and you have typed ht.n, there will be two methods beginning with notify (notify() and notifyAll()). To more quickly narrow the selection to just notifyAll(), press Tab to expand ht.n to ht.notify and then type A. You can then press Enter to complete the statement with notifyAll().

Disabling Automatic Appearance of the Java Code Completion Box

If you find the code completion box to be more of a nuisance than a help, you can disable automatic appearance of the code completion popup. Code completion will still work if you manually activate it by pressing Ctrl-spacebar or Ctrl-\.

You can also leave automatic appearance of the code completion popup enabled but disable the bulkier Javadoc code completion dialog box. The Javadoc popup can be manually invoked with Ctrl-Shift-spacebar.

To disable automatic appearance of the code completion box:

  1. Choose Tools | Options, click Editor in the left panel, and select the General tab.
  2. Deselect the Auto Popup Completion Window property and click OK.

To disable automatic appearance of the Javadoc popup when you use the code completion box:

  1. Choose Tools | Options, click Advanced Options, expand the Editing | Editor Settings node, and select the Java Editor node.
  2. In the Expert settings, deselect the Auto Popup Javadoc Window property.

Changing Shortcuts for Code Completion

If you prefer to use different shortcuts for code completion, you can change those shortcuts in NetBeans IDE:

  1. Choose Tools | Options, click Editor in the left panel, and select the Keymap tab.
  2. Expand the Other folder and select the command for which you want to change the shortcut.

    Commands that apply to code completion are Show Code Completion Popup, Show Code Completion Tip Popup, and Show Documentation Popup.

  3. Click Add, and type the shortcut that you want to use.

    You can remove an already assigned keyboard shortcut by selecting the shortcut and clicking Remove.

  • + Share This
  • 🔖 Save To Your Account