17 Getting Started with Developing Java Applications

This chapter provides an overview of the tools and features that JDeveloper provides to help you develop Java applications.

This chapter includes the following sections:

17.1 About Developing Java Applications

JDeveloper enables you to build and assemble Java applets and client applications using JavaBeans, and interactive, desktop-based GUI applications using Swing and AWT components. You can also create and run Java client applications with Java Web Start within the JDeveloper IDE.

JDeveloper provides resources for editing, optimizing, running, and debugging Java code:

17.2 About the Java Source Editor

The Java Source Editor displays Java source files, and facilitates editing of Java code. The Java Source editor is a specialized form of the generic Source Editor that JDeveloper provides for editing source code across several technologies, including XML, JSP, and HTML.

In additional to the Java-specific features of the Java Source Editor, you can also use the common set of features that JDeveloper provides to enhance coding across all domains. These features are available through the context menu or the Source menu.

Double-clicking a node in the Application Navigator either opens or brings the default editor to the foreground. When a file is open in the Source Editor, its corresponding elements are displayed hierarchically in the Structure window. Double-clicking a node in the Structure window shifts the focus to the definition of that element in the Source Editor.

You can customize the behavior of the Java Source Editor by specifying preferences in the Preferences Dialog.

17.3 Understanding Java Source Editor Features

The Java Source Editor provides features to enable easier and quicker navigation through code.

17.3.1 Using Code Insight

With Java Code Insight, you can filter out information not likely to be as useful to you (such as top-level packages, imported classes, default Object methods, deprecated items) and emphasize the information that you'll want to focus on (local variables, locally declared members, overloaded methods).

You can configure member insight, the Java-specific implementation of Code Insight's completion insight, and you can choose to display deprecated members or not in Code Insight's parameter insight window.

Member insight provides you with a list of which instance and static members (fields, methods, inner classes) are accessible from a given statement context. For example, it tells you which methods you can call from any given method.

You can use Code Insight to speed up the process of writing code. Code Insight has two varieties: completion insight and parameter insight. You can enable or disable each independently and set the delay in seconds for each to appear when the cursor is paused at an appropriate insertion point.

To invoke completion after typing the period separator or, in the default keymap, press Ctrl+Space. To invoke parameter insight, pause after typing an opening (the left) parenthesis or, in the default keymap, press Ctrl+Shift+Space. To exit either type of insight at any time, press Esc. Note that if you change your keymapping, these keyboard accelerators may change. You can click QuickDoc, located at the bottom right of the completion insight list, to display the Javadoc for the currently selected element

After a method has been completed by completion insight, the source editor automatically fills in the parameters based on the method code. You can tab between these parameters, and edit them manually or using parameter insight.he source editor will automatically add an import if it can find only one exact match for an unresolved reference to a class. You can set preferences for this feature in the Preferences Dialog.

To change Code Insight settings or to view or change accelerators, from the main menu choose Tools > Preferences to open the Preferences dialog and then navigate to the appropriate page. For more information, see Section 17.4.1, "How to Set Code Insight Options for the Java Source Editor"

17.3.1.1 Adding Annotations to Your Java Code

Use the Code Insight feature to quickly add annotations to your Java code. An annotation is used to associate information with a program element. Annotations can be used in classes, fields, methods, parameters, local variables, constructors, enumerations, and packages. To add annotations in your Java code: declare the annotation, create a function, and then add your annotations.

When you start adding an annotation, Member Insight (Ctrl-Space) displays a list of options (fields, members, classes) based on the statement context. Parameter Insight (Ctrl-Shift-Space) displays information about the annotation like the names of the elements of the annotation type, the default values, and the created values. It also highlights the element currently under the cursor in the annotation.

For more information see Section 18.5, "How to Customize Javadoc Options for the Java Source Editor."

17.3.2 Using Code Peek

You can hold down the Shift key and then hover over a variable or method to show its definition in a ghost window. This feature makes it convenient to quickly view code without moving cursor focus from your current code.

17.3.3 Using Scroll Tips

While dragging the vertical scroll bar, a small tip window appears next to the bar, revealing the methods that are visible or partially visible on screen. This enables you to more easily see what methods are in view while quickly scrolling. You can also see the name of the method whose beginning is not immediately in view.

17.3.4 Searching Incrementally

To search incrementally, from the main menu choose Search > Incremental Find Forward or Search, then Incremental Find Backward. In the dialog that appears, begin typing. As you type, the cursor jumps to the next instance of that particular letter combination, either forward or backward. The search does not support wildcards.

17.3.5 Using Shortcut Keys

Shortcut keys, or accelerators, are combinations of keys that you can use to navigate or to perform certain operations using the keyboard instead of the mouse. You can select from a variety of predefined keymaps or define your own accelerators.

To view or change existing accelerators, to define new accelerators, or to load preset keymaps, from the main menu choose Tools, then Preferences to open the Preferences dialog and then navigate to the Shortcut Keys page. To view or change accelerators for the editor, select Code Editor from the Category list.

Note that block commenting is indicated by Toggle Line Comments. It is defined in the default keymap as Ctrl-Shift-Slash or Ctrl-Slash.

17.3.6 Bookmarking

While bookmarking code:

  • You can see a list of all bookmarks you have created in a Bookmarks window. This window appears when you click the Go to Bookmark icon. This window also displays the line number and method name that contains the bookmark.

  • You can create numbered bookmarks using the keyboard shortcut Ctrl-Shift-number. You can quickly navigate to that bookmark with Ctrl-number.

17.3.7 Browsing Java Source

To navigate to the source for any identifier in an open Java file, right-click on the identifier you would like to browse and choose Go to Declaration. Alternatively, you can hold down the Ctrl key and click on an identifier to navigate to its source. If the source is not available, JDeveloper will reverse-engineer the class file.You may browse imported classes and interfaces, member fields and methods, and local variables. If you are browsing a method or constructor invocation, this declaration search will resolve the types in order to determine the correct method or constructor invocation. For instance, the code in Example 17-1 revokes the declaration search at SetText. It brings up the source code for javax.swing.JButton, with the SetText() method displayed.

Example 17-1 Revoking the Declaration Search

import javax.swing.JButton
...
JButton b1 = new JButton();
...
b1.SetText: ('OK');

If the identifier cannot be browsed or if there is nothing at that cursor position, this search command on the context-sensitive menu will be disabled. If JDeveloper is unable to locate the appropriate location to jump to or if the identifier cannot be browsed due to access restrictions (for example, private members), the Java Source Editor's status bar will display a message indicating so.

17.3.8 Using Code Templates

Code templates are sections of pre-written code that can be conveniently inserted into source file to avoid typing it in manually. Templates can intelligently modify the inserted code to suit its surrounding code, and imports required by code templates are automatically imported. You can use shortcuts to speed up the selection of the required template. Pressing Ctrl+Enter anywhere in the source file brings up a list of code templates that you can select. The templates provided in this list are contextual and only those suitable for the current location are offered. You can click QuickDoc on the bottom right corner of this list to see the structure of the selected code template.If you were using the existing template for the for loop, for instance, you would type for and then (in the default keymapping) press Ctrl+Enter. The template would then be filled in as follows:

for (;;)

A complete list of all code templates is available in the Code Editor Help.To edit or create code templates, or to view or change accelerators, from the main menu choose Tools > Preferences to open the Preferences dialog and then navigate to the appropriate page.

For more information, see Section 18.3.9, "How to Use Code Templates."

17.4 Setting Preferences for the Java Source Editor

You can customize the behavior of the Java Source Editor using the Preferences Dialog.

You can also use the Preferences Dialog to specify settings for the general source editing environment.

17.4.1 How to Set Code Insight Options for the Java Source Editor

You can set various Insight options to create the behavior you want.

To set the options for Code Insight as it applies to Java:

  1. From the main menu, choose Tools > Preferences.

  2. In the Preferences dialog, expand the Code Editor node.

  3. Expand the Java node and select Code Insight.

  4. On the Java Insight page, set the options you want.

  5. Click OK.

17.4.2 How to Set Comment and Brace-Matching Options for the Java Source Editor

JDeveloper enables you to set comment and brace-matching options for the Java source editor.

To set the options for Java comment and brace matching in the source editor:

  1. From the main menu, choose Tools > Preferences.

  2. In the Preferences dialog, expand the Code Editor node.

  3. Select the Display node.

  4. On the Display page, enable or disable automatic brace matching and set the delay time.

  5. Click OK.

  6. Reopen the Preferences dialog, expand the Code Editor node, and select the Java node.

  7. On the Java page, set the attributes for comments and brace matching to create the behavior that you want.

  8. Click OK.

Note that block commenting is an accelerator function. In the default keymap, use Ctrl+Shift+/ or Ctrl+/ to block-comment Java code.

17.4.3 How to Enable Automatic Import Assistance for the Java Source Editor

You can view assistance that enables you to organize import statements in the Java Source Editor.

To enable assistance for automatically adding import statements:

  1. From the main menu, choose Tools > Preferences.

  2. In the Preferences dialog, expand the Code Editor node.

  3. Expand the Java node and select Imports.

  4. On the Imports page, select Enable Auto-Popup for Import Assistance.

  5. Click OK.

17.4.4 How to Set Import Statement Sorting Options for the Java Source Editor

You can set options to sort import statements in the Java Source Editor.

To set the options for sorting import statements:

  1. From the main menu, choose Tools > Preferences.

  2. In the Preferences dialog, expand the Code Editor node.

  3. Expand the Java node and select Imports.

  4. On the Imports page, set the attributes to create the behavior that you want when sorting import statements in the editor.

  5. Click OK.

17.5 Using Toolbar Options

The Java Source Editor displays Java source files, and facilitates editing of Java code. Icons that perform various features are located at the top of the Java Source Editor, as described in Table 17-1.

Table 17-1 Toolbar Options

Icon Name Description

Quick Outline

Quick Outline

Click to display a tree of the available methods and fields of the current class and its super classes.

Clicking this icon brings up the Quick Outline window (for more information, see Section 17.6, "Using the Quick Outline Window"). This window floats just above the code and contains a tree of the available methods and fields of the current class and its super classes. You can instantly start typing in a filter field to reduce the visible items, allowing quick and easy selection for navigation to the desired place

Code Highlight

Code Highlight

Click to highlight all instances of the code component that the cursor is currently placed on.

Clear All Highlighting

Clear All Highlighting

Click to clear all highlighting.

Generate Accessors

Generate Accessors

Click to insert get and set methods into a class, using the Generate Accessors dialog.

Override Methods

Override Methods

Click to override inherited methods for the class in focus.

Implement Interfaces

Implement Interfaces

Click to modify a target class to implement one or more interfaces, or to make a target interface extend one or more other interfaces, using the Implement Interface dialog.

Reformat

Reformat

Click to apply source formatting to your code.

Surround

Surround

Click to surround the currently selected block of text in the Java Source Editor with a coding construct, using the Surround With dialog.

Toggle Bookmark

Toggle Bookmark

Click to insert or remove a bookmark on the line of code currently in focus.

Go to Next Bookmark

Go to Next Bookmark

Click to place the cursor at the next bookmark.

Go to Previous Bookmark

Go to Previous Bookmark

Click to place the cursor at the previous bookmark.

Show Selected Element Only

Show Selected Element Only

Click to view only one particular element in the editor. You can use this feature to tightly focus on a method, class, inner class, or field declaration. A message at the bottom of the file reminds you that the Show Selected Element mode is currently active.

Block Coloring

Block Coloring

Click to activate block coloring. You can use this feature to highlight blocks of code for better readability. Coloring preferences can be set using the Preferences Dialog.


17.6 Using the Quick Outline Window

Clicking the Quick Outline Toolbar icon directly to the right of the Find field brings up the Quick Outline window shown in Figure 17-1. This window floats just above the code and contains a tree of the available methods and fields of the current class and its super classes. You can instantly start typing in a filter field to reduce the visible items, allowing quick and easy selection for navigation to the desired place.

Figure 17-1 Quick Outline Window

Quick Outline Window

Table 17-2 lists the available icons and options.

Table 17-2 Quick Toolbar Icons

Icon Name Description

Show Methods

Show Methods

Click to display methods and constructors. The default is methods, fields, and static members all displayed.

Show Fields

Show Fields

Click to display fields. The default is methods, fields, and static members all displayed.

Show Static Members

Show Static Members

Click to display static members. The default is methods, fields, and static members all displayed.

Show Public Members Only

Show Public Members Only

Click to display only public members. The default value is deselected.

Show Inherited Member

Show Inherited Member

Click to display only inherited members.

Sort Alphabetically

Sort Alphabetically

Click to sort class members alphabetically. The default value is deselected.

Sort by Type

Sort by Type

Click to sort class members first by type (in this order: constructors, methods, fields, inner classes), and then alphabetically within those categories. The default value is selected.

Click the down arrow next to Sort Alphabetically to view the option.

Sort by Access

Sort by Access

Click to sort class members first by access modifier, and then alphabetically within those categories. The default value is deselected.

Click the down arrow next to Sort Alphabetically to view the option


17.7 About the Java UI Visual Editor

The Java UI Visual Editor displays the visual components of a user interface in Editing mode. Note: You can use the Java Visual Editor for Swing/AWT Applications only.

When a Java Visual Editor is open, its corresponding elements are displayed hierarchically in the Structure window. If the Property Inspector is open, selecting elements in either the Structure window or the Java Visual Editor changes the selection in the Inspector as well.

The Java Visual Editor displays a GUI hierarchy. If these are menu items, the hierarchy is displayed in one fashion; if these are nonmenu items, it is displayed in another. The mode of presentation differs, as the sort of editing that you are engaged in differs. For more information, see Section 18.2.9, "How to View the Hierarchy of a Class or Interface."

When the node is selected in the Navigator, its GUI structure also displays in the Structure window. All nonmenu GUI items for this object appear under a node labeled UI. Any menu items appear under a node labeled Menu. Any non-GUI items appear under a node labeled Other. Once you have opened the Java Visual Editor for an object in the Navigator, to switch between the display of nonmenu GUI elements and menu elements you have only to click on a node below these UI or Menu nodes in the Structure window.

Because the display in the Java Visual Editor is rooted in the GUI hierarchy, when you click on a node (for all GUI objects) in the Structure window what loads into the editor is the visual representation of the root node and all its descendants: the entire hierarchy opens, regardless of which node in the hierarchy you selected. What displays in the editor reflects the complete GUI hierarchy; the specific element selected in the display reflects the specific node selected in the window. Selections in the Structure window and the Java Visual Editor are kept in synch.

If you have an orphan node in the Structure window, that node and its descendants comprise the entire hierarchy, with the orphan being the root of the hierarchy. The Java UI display will reflect this. If you had a control, for instance, that was not parented, and you selected the node for that control, the control and any descendants would now appear in the editor, without a container. Any changes you make to that control, however, will result in generated code.

Right-click anywhere within the Java Visual Editor to bring up a context-sensitive menu of commands. The context menus differ, depending upon whether you are editing nonmenu or menu items, and the commands available within the context menu depend on the selected object.

17.7.1 Java Swing and AWT Components

Use Swing and AWT JavaBeans components to assemble the user interface (UI) for a Java application or applet. You construct the UI in the Java Visual Editor by selecting JavaBeans from the Component Palette, such as buttons, text areas, lists, dialogs, and menus. Then, you set the values of the component properties and attach event-handler code to the component events. Tools to visually design and program Java classes to produce new compound or complex component.

For more information, see Section 20.1, "About Implementing Java Swing User Interfaces."