- Opening the Source Editor
- Managing Automatic Insertion of Closing Characters
- Displaying Line Numbers
- Generating Code Snippets without Leaving the Keyboard
- Using Code Completion
- Inserting Snippets from Code Templates
- Using Editor Hints to Generate Missing Code
- Matching Other Words in a File
- Generating Methods to Implement and Override
- Generating JavaBeans Component Code
- Creating and Using Macros
- Creating and Customizing File Templates
- Handling Imports
- Displaying Javadoc Documentation While Editing
- Formatting Code
- Text Selection Shortcuts
- Navigating within the Current Java File
- Navigating from the Source Editor
- Searching and Replacing
- Deleting Code Safely
- Changing a Method's Signature
- Encapsulating a Field
- Moving a Class to a Different Package
- Moving Class Members to Other Classes
- Creating a Method from Existing Statements
- Creating an Interface from Existing Methods
- Extracting a Superclass to Consolidate Common Methods
- Changing References to Use a Supertype
- Unnesting Classes
- Tracking Notes to Yourself in Your Code
- Comparing Differences Between Two Files
- Splitting the Source Editor
- Maximizing Space for the Source Editor
- Changing Source Editor Keyboard Shortcuts
Navigating within the Current Java File
The IDE provides several mechanisms to make it easier to view and navigate a given Java file:
- The Navigator window, which appears below the Projects window and provides a list of members (for example, constructors, fields, and methods) in the currently selected Java file.
- Bookmarks, which enable you to easily jump back to specific places in the file.
- The Alt-K and Alt-L "jump list" shortcuts, mentioned in Jumping Between Areas Where You Have Been Working later in this chapter.
- Keyboard shortcuts to scroll the window. See Table 5-5 in the following section.
Table 5-5. Cursor and Scrolling Shortcuts
Description
Shortcut
Moves the insertion point to the next word (previous word).
Ctrl-Right (Ctrl-Left)
Moves the insertion point to the top (bottom) of the file.
Ctrl-Home (Ctrl-End)
Scrolls up (down) without moving the insertion point.
Ctrl-Up (Ctrl-Down)
Scrolls the window so that the current line moves to the top of the window.
Alt-U, then T
Scrolls the window so that the current line moves to the middle of the window.
Alt-U, then M
Scrolls the window so that the current line moves to the bottom of the window.
Alt-U, then B
Moves the insertion point to the parenthesis, bracket, or curly brace that matches the one directly before your insertion point.
Ctrl-[
- The code folding feature, which enables you to collapse sections of code (such as method bodies, Javadoc comments, and blocks of import statements), thus making a broader section of your class visible in the window at a given time.
Viewing and Navigating Members of a Class
The IDE's Navigator window (shown in Figure 5-10) provides a list of all "members" (constructors, methods, and fields) of your class. You can double-click a member in this list to jump to its source code in the Source Editor. Alternatively, instead of using the mouse, press Ctrl-7 to give focus to the Navigator window. Then begin typing the identifier until the Navigator locates it and press Enter to select that identifier in the Source Editor.
Figure 5-10 Navigator window
You can use the filter buttons at the bottom of the window to hide non-public members, static members, fields, and/or inherited members.
Moving the Insertion Point and Scrolling the Window
There is a wide range of shortcuts that you can use for moving the insertion point around and scrolling the Source Editor without moving the insertion point. See Table 5-5 for a list of some of the most useful file navigation shortcuts.
Bookmarking Lines of Code
You can set bookmarks in files to make it easy to find an area of the file that you are working with frequently. You can then cycle through the file's bookmarks by pressing F2 (next bookmark) or Shift-F2 (previous bookmark).
To bookmark a line in a file, click in the line and press Ctrl-F2. To remove a bookmark, also use Ctrl-F2.
Hiding Sections of Code
You can collapse (or fold) low-level details of code so that only one line of that block is visible in the Source Editor, leaving more room to view other lines. Methods, inner classes, import blocks, and Javadoc comments are all foldable.
Collapsible blocks of code are marked with the icon in the left margin next to the first line of the block. The rest of the block is marked with a vertical line that extends down from the icon. Collapsed blocks are marked with the icon. You can click one of these icons to fold or expand the particular block it represents. See Figure 5-11 for an example.
Figure 5-11 Examples of expanded and folded code in the Source Editor
You can also collapse and expand single or multiple blocks of code with keyboard shortcuts and menu items in the Edit | Code Folds menu and the Code Folds submenu in the Source Editor. See Table 5-6 for a list of these commands and shortcuts.
Table 5-6. Code Folding Commands
Command |
Shortcut |
Collapse Fold |
Ctrl-NumPad-Minus |
Expand Fold |
Ctrl-NumPad-Plus |
Collapse All |
Ctrl-Shift-NumPad-Minus |
Expand All |
Ctrl-Shift-NumPad-Plus |
Collapse All Javadoc |
none |
Expand All Javadoc |
none |
Collapse All Java Code (collapses everything except Javadoc documentation) |
none |
Expand All Java Code (expands everything except Javadoc documentation) |
none |
By default, none of the code that you write is folded. You can configure the Source Editor to fold Java code by default when you create a file or open a previously unopened file.
To configure the IDE to fold certain elements of Java code automatically:
- Choose Tools | Options, click the Editor panel, and select the General tab.
- Where it says Collapse By Default, select the checkboxes for the elements that you want folded by default. You can choose from methods, inner classes, imports, Javadoc comments, and the initial comment.