Home > Articles > Programming > Java

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

Formatting Code

When you type or have code generated in the Source Editor, your Java code is automatically formatted in the following ways by default:

  • Members of classes are indented four spaces.
  • Continued statements are indented eight spaces.
  • Any tabs that you enter are converted to spaces.
  • When you are in a block comment (starting with /**), an asterisk is automatically added to the new line when you press Enter.
  • The opening curly brace is put on the same line as the declaration of the class or method.
  • No space is put before an opening parenthesis.

If your file loses correct formatting, you can reformat the whole file by selecting Source | Reformat Code (Ctrl-Shift-F). If you have any lines selected, the reformatting applies only to those lines.

If you have copied code, you can have it inserted with correct formatting by pasting with the Ctrl-Shift-V shortcut.

Indenting Blocks of Code Manually

You can select multiple lines of code and then indent all those lines by pressing Tab or Ctrl-T.

You can reverse indentation of lines by selecting those lines and then pressing Shift-Tab or Ctrl-D.

Changing Formatting Rules

For various file types, you can adjust formatting settings, such as for number of spaces per tab, placement of curly braces, and so on.

To adjust formatting rules for Java files:

  1. Choose Tools | Options.
  2. Click Editor in the left panel and select the Indentation tab.
  3. Adjust the properties for the indentation engine to your taste.
  4. Reformat each file to the new rules by opening the file and pressing CtrlShift-F (with no text selected).

For other file types, you can change formatting properties in the Advanced Options part of the Options Window and by adjusting that file type's indentation engine.

To change formatting rules for non-Java file types:

  1. Choose Tools | Options and click Advanced Options.
  2. Expand Editing | Indentation Engines, and select the indentation engine for the file type for which you want to modify formatting rules.

    If there is no specific indentation engine for your file type, find out which indentation is being used for that file type by expanding Editing | Editor Settings, selecting the editor type, and looking at the value of its Indentation Engine property.

  3. Adjust the properties for the indentation engine to your taste.
  4. Reformat each file to the new rules by opening the file and pressing CtrlShift-F (with no text selected).

There are other preset indentation engines available (the "simple" and "line wrapping" indentation engines) you might prefer to use.

To change the indentation engine that is used for a file type:

  1. Choose Tools | Options and click Advanced Options.
  2. Expand Editing | Editor Settings and select the node for the editor type for which you want to change the indentation engine.
  3. Select the indentation engine from the Indentation Engine property's combo box.
  4. Reformat each file to the new rules by opening the file and pressing CtrlShift-F (with no text selected).

To create a new indentation engine:

  1. Choose Tools | Options and click Advanced Options.
  2. Expand Editing | Indentation Engines, right-click the node for the indentation engine on which you want to base your new indentation engine, and choose Copy.
  3. Right-click the Indentation Engines node and choose Paste | Copy.
  4. Modify the name of the indentation inline and adjust the properties to your taste.
  5. In the Advanced Options part of the Options dialog box, expand Editing | Editor Settings and select the node for the editor (such as Java Editor or HTML Editor) that you want the indentation engine to apply to.

Changing Fonts and Colors

You can adjust the fonts that are used in the Source Editor and the way colors and background highlighting are used to represent syntactic elements of your code. You can make changes that apply to all file types and changes that apply to specific file types.

To make changes in fonts and colors that are reflected throughout all editor types:

  1. Choose Tools | Options and click the Fonts & Colors panel.
  2. In the Languages drop-down list, select All Languages.
  3. In the Category list, select Default.
  4. Use the Font, Foreground, Background, Effects, and Effect Color fields to change the appearance of that code element.

    These changes should be reflected in the syntax categories for all the languages, since other categories are essentially designed as customizations of this one.

To change fonts and colors for a specific code syntax element:

  1. Choose Tools | Options and click the Fonts & Colors panel.
  2. Select the editor type from the Languages drop-down list.

    If the syntax element applies to multiple languages, select All Languages.

  3. Select a syntax element in the Category list.
  4. Use the Font, Foreground, Background, Effects, and Effect Color fields to change the appearance of that code element.
  • + Share This
  • 🔖 Save To Your Account