Tải bản đầy đủ (.pdf) (14 trang)

Creating and Editing Files

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (98.7 KB, 14 trang )

Chapter 2 Creating and Editing Files 21
CHAPTER
2
Creating and Editing Files
Creating and editing Java source code is the most important function that the IDE
serves, since that's what developers generally spend most of their day doing.
NetBeans IDE provides a wide range of tools that can complement any developer's
personal style, regardless of whether you prefer to code everything by hand or want
the IDE to generate large chunks of code for you.
This section covers the following topics:

Creating Java Files

Using File Templates

Using GUI Templates

Editing Java Files in the Source Editor

Code Completion

Code Templates

Special Code Template Syntax

Editor Hints

Refactoring

Working With Import Statements


Formatting Java Source Code

Navigating in the Source Editor

Navigating Within a Java File

Search and Selection Tools

Navigating Between Documents

Configuring the Editor
Using NetBeans
TM
5.0 IDE
22 Chapter 2 Creating and Editing Files
Creating Java Files
NetBeans IDE contains templates and wizards that you can use to create all kinds of
source files, from Java source files to XML documents to resource bundles.
Perhaps the easiest way to create a file (once you have already created a project) is to
right-click the project node of the project for which you want to create the file in the
Projects window. You can then choose the desired file type from the New pop-up
menu when you right-click the project node. The New submenu contains shortcuts
to commonly-used templates and a File/Folder command that you can use to open
the New File wizard and access all NetBeans templates.
Choosing a template from the New menu
The New File wizard enables you to create a new file based on the IDE’s default file
templates. The file templates are grouped by type. In addition to the default file
templates, you can customize the templates the IDE uses to create files and also
create your own templates. Having the option to use your own templates can be
useful if a certain file type needs to have standard elements, or you want to change

the way other elements are generated. When you create your own templates, you
can make them available in the New File wizard.
Using File Templates
You use the Template Manager to modify and create new templates by choosing
Tools from the main menu and choosing Template Manager. You can create a new
template by copying an existing template and then clicking Edit. For example, if
you want to create a new Java class template, you can duplicate an existing Java
class template, then select the new class and then click Open in Editor. You can
now modify the class in the Source Editor and save it. The new class is now
available in the New File wizard.
Using NetBeans
TM
5.0 IDE
Chapter 2 Creating and Editing Files 23
If you have an existing template that you would like to add to the IDE, click Add
and locate the file on your system. The file is now available as a template in the
New File wizard.
Using GUI Templates
If you want to visually edit a Java GUI form using the IDE's GUI Builder, you
have to create the form's source file using the IDE's Java GUI Forms templates.
This template group contains templates for AWT and Swing forms. For example,
you cannot create a normal Java class file and then change it to extend JPanel and
edit it in the GUI Builder.
For more information about creating Java GUIs in the IDE, see the following:

GUI Building in NetBeans IDE 5.0
Editing Java Files in the Source Editor
The Source Editor is your main tool for editing source code. It provides a wide range
of features that make writing code simpler and quicker, like code completion,
highlighting of compilation errors, syntax highlighting of code elements, as well as

other advanced formatting and search features.
Although the Source Editor can be considered a single IDE component, it is really a
collection of editors. Each type of source file has its own editor that provides
different functionality. In this section we'll be dealing with the Java editor, but many
of the same concepts apply to other editors. To open a Java source file in the Source
Editor, double-click the file's node in the Projects window or Files window.
The IDE has many mechanisms for generating different types of code snippets. The
following mechanisms are some of the most commonly used.

Code Completion (Ctrl-Space). When you are typing your code, you can use the
shortcut to open up the code completion box. The code completion box contains a
context-sensitive list of options to complete the statement you are currently
typing. Continue to type additional characters to narrow down the number of
options presented in the code completion box.

Code Templates. For many commonly used code snippets you can use multi-
keystroke abbreviations instead of typing the entire snippet. The IDE expands the
abbreviation into the full code snippet after you press the spacebar.

Editor Hints (Alt-Enter). If the IDE detects an error, such as missing code, the IDE
can suggest missing code to fix the error, and then insert that code where
necessary. When the insertion point is in the line marked as containing an error,
the IDE displays a lightbulb icon in the margin to indicate a suggested fix for that
line. Use the keyboard shortcut or click the lightbulb to display the suggestion.
Select the hint you want and press Enter to have the fix generated in your code.
Using NetBeans
TM
5.0 IDE
24 Chapter 2 Creating and Editing Files
The following topics illustrate how to get the most out of these features.

Code Completion
When typing Java identifiers in the Source Editor, you can use the IDE’s code
completion box to help you finish expressions. When the code completion box
appears, a box with Javadoc documentation also appears displaying any
documentation for the currently selected item in the code completion box. You
can disable the Javadoc box in the Options window.
Code completion example
You can use the code completion box to generate a variety of code, including the
following:

Fill in the names of classes and class members, as well as any necessary import
statement.

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 Configuring the Editor
below for more information.

Generate getter and setter methods.
To open the code completion box, type the first few characters of an expression
and then press Ctrl-space. Alternately, you can open the code completion box by
pausing after typing a period (.) in an expression. The code completion box opens
with a selection of possible matches for what you have typed so far. You can
narrow the selection in the code completion box by typing additional characters
in the expression.
Using NetBeans
TM
5.0 IDE
Chapter 2 Creating and Editing Files 25

To use the code completion box to complete the expression, continue typing until
there is only one option left and press Enter, or scroll through the list and select
the option you want and then press Enter. To close the code completion box
without entering any selection, press Esc. To turn off code completion in the
Source Editor, see Configuring the Editor.
The IDE uses the classes on your compilation classpath to provide suggestions for
code completion and other features. Classes from the target JDK version, other
commonly used project-specific APIs like the Servlet, JSP, JSTL and XML APIs, as
well as the sources you have manually added to the classpath can be used in code
completion. For details, see Managing a Project's Classpath.
Code Templates
You can use code templates to speed up the entry of commonly used sequences of
reserved words and common code patterns, such as for loops and field
declarations. The IDE comes with a set of templates, and you can create your own
code templates in the Options window. For more on how to configure how code
templates are implemented in the Source Editor, see Configuring the Editor. For
more on the syntax used for creating your own code templates, see Special Code
Template Syntax.
A code template can be composed of bits of commonly used text, or it can be
more dynamic, generating a skeleton and then letting you easily tab through it to
fill in the variable text. Where a code snippet repeats an identifier (such as an
object name in an instance declaration), you just have to type the identifier name
once.
For example, if you enter forc and press the space bar, it expands into
for (Iterator it = collection.iterator(); it.hasNext();) {
Object elem = (Object) it.next();
}
Once the code is expanded in the Source Editor, you can simply press tab to jump
to the next variable in the code snippet.
If an abbreviation is the same as the text that you want to type and you do not

want it to be expanded into something else, press Shift-spacebar to keep it from
expanding.
You can access code templates by doing the following in the Source Editor:

Typing the first few letters of the code, pressing Ctrl-spacebar, and then
selecting the template from the list in the code completion box. The Javadoc box
displays the full text of the template.

Typing the abbreviation for the code template directly in the Source Editor and
then pressing the spacebar. You can find the abbreviations for the built-in Java
code templates by opening the Editor settings in the Options window and
choosing the Code Templates tab.

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×