Fusion Middleware Documentation
Advanced Search


Developing Applications with Oracle JDeveloper
Close Window

Table of Contents

Show All | Collapse

10 Getting Started with Developing Java Applications

This chapter is an overview of the tools and features that JDeveloper provides to speed up the process of writing Java code.

This chapter includes the following sections:

10.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.

10.2 Using 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.

Double-clicking a node in the Applications window 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. For more information, see Section 10.12, "Setting Preferences for the Java Source Editor."

10.3 Using Code Insight

With Java Code Insight, you can filter out unnecessary information such as top-level packages, imported classes, default Object methods, deprecated items, and emphasize more key detail such as local variables, locally declared members, overloaded methods.

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. In the following method.

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.

You can also use and 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.

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 11.3.1, "Editing Code with the Java Visual Editor"

10.3.1 Using Code Insight to 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 11.4, "How to Add Documentation Comments."

10.4 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.

Figure 10-1 Code Peek

Code Peek

10.5 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.

10.6 Using InfoTips

An InfoTip is a pop-up window that reveals additional detail about an item in the application, for example, text in the Java Source Code editor and the overview gutter. You display the InfoTip by pointing or hovering over the item with your mouse cursor. It remains visible until you move the mouse away or click elsewhere in the UI.

Figure 10-2 InfoTips

InfoTips

10.7 Searching Incrementally

To search incrementally, from the main menu choose Search > Incremental Find Forward or Search > Incremental Find Backward. Begin typing in the dialog that displays. 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.

10.8 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.

For more information, see Section 3.5, "Keyboard Navigation in JDeveloper."

10.9 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.

    Figure 10-3 Go to Bookmark Icon

    Go to Bookmark Icon
  • You can create numbered bookmarks using the keyboard shortcut Ctrl-Shift-<number>. You can quickly navigate to that bookmark by pressing Ctrl-<number>.

For more information, see Section 3.8.7, "How to Set Bookmarks in Source Files."

10.10 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 10-1 revokes the declaration search at SetText. It brings up the source code for javax.swing.JButton, with the SetText() method displayed.

Example 10-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.

10.11 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:

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 11.3.9, "How to Use Code Templates."

10.12 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. For more information, see Section 3.8.2, "How to Set Preferences for the Source Editor."

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.

    A list of selectable options displays, for example, Bookmarks and Code Templates.

  3. Click an option and choose options that appear on the new page that displays.

  4. Click OK when you are finished.

10.12.1 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. For example, you can choose to add leading and closing asterisks as you add new lines either in multi-line Java comments or in Javadoc comments.

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.

    For example, you can select the Add Closing Bracket or Parenthesis Automatically option.

  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.

10.12.2 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, choose options to create the behavior that you want when sorting import statements in the editor.

  5. Click OK.

10.12.3 How to Choose a Coding Style

You can use the Tools > Preferences dialog to select a profile that determines the style of code shown in the Java Source Code editor. The coding style determines Java Code conventions such as formatting, how import statements display in code, variable names, and member order.

To select a coding style for the Java Source Code editor:

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

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

  3. Click the Code Style node.

  4. On the Code Style page, choose a Profile.

  5. Click OK.

10.12.3.1 How to Edit a Coding Style

You can edit an existing coding style and make it available as a selectable Profile option on the Code Style page. You can also import an existing code style profile from an XML file.

To edit an existing code profile:

  1. Follow the steps in Section 10.12.3, "How to Choose a Coding Style" to choose an existing profile in the Profile list.

  2. Click Save As and enter a name for your new profile.

  3. Click Edit.

  4. In the Edit Code Style Profile - Java Code Conventions page, choose options for your new profile.

  5. Click OK.

  6. You can alternately click Import to import a code style profile into JDeveloper from an XML file.

10.12.3.2 How to Set Up a Coding Style Using an Extension

You can create a custom coding profile using an extension, then set it as the default for a role, for example, developer, using the extension.xml file. For more information, see "Introduction to Developing Oracle JDeveloper Extensions" in Oracle Fusion Middleware Developer's Guide for Oracle JDeveloper Extensions.

To set a default coding profile using an extension:

  1. Edit a custom style according to Section 10.12.3.1, "How to Edit a Coding Style."

  2. Choose Export from the More Actions list.

  3. Add it to an extension and put an entry in the extension.xml file, similar to Example 10-2.

    Example 10-2 extension.xml file

    <hooks>
      <code-style xmlns="http://xmlns.oracle.com/ide/extension">
        <profile-url>/META-INF/custom-profile.xml<profile-url>
      </code-style>
    <hooks>
    
  4. To set the profile as the default style, add a section to the role shaping file with the profile name, similar to Example 10-3.

    Example 10-3 Setting the profile as the default style

    <c:settings-ui-customizations>
      <c:page idref="/preferences/ceditor/style">
        <c:field idref="CodingStylePreferences.profile">
           <c:value>Custom Profile</c:value>
        </c:field>
      </c:page>
    </c:settings-ui-customizations> 
    

10.13 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 10-1.

Table 10-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 10.14, "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.


10.14 Using the Quick Outline Window

Clicking the Quick Outline Toolbar icon (Figure 10-4) to the right of the Find field in the Java source editor displays the Quick Outline window shown in Figure 10-5.

Figure 10-4 Quick Outline Icon

Quick Outline Icon

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 place.

Figure 10-5 Quick Outline Window

Quick Outline Window

Table 10-2 lists the available icons and options.

Table 10-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


10.15 Working with the Java UI Visual Editor

The Java UI Visual Editor displays the visual components of a user interface in editing mode. For more information, see Section 11.3.1, "Editing Code with the Java Visual Editor."

Note:

You can use the Java Visual Editor for Swing/AWT Applications only.

10.15.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 Components window, 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 15.2, "About Java Swing UI Components and Containers".