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

7.1 About Developing Java Applications

From the JDeveloper IDE, use Swing and AWT components to build, deploy, and publish desktop-based GUI applications, and use Java Web Start to build Java client 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.

7.2 Using the Java Source Editor

JDeveloper displays Java source files in the Java Source Editor. Use the editor to edit Java code, and use the Preferences Dialog to customize the 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.

7.3 Using Code Insight

Use Java Code Insight to filter irrelevant information, to speed up coding, to complete code using completion insight, to select instance and static members using member insight, and to edit parameters using parameter insight. Use Preferences from Tools to edit Code Insight settings, or to view or change accelerators.

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 keymap ping, 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. The 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.

7.3.1 Using Code Insight to Add Annotations to Your Java Code

An annotation associates information with a program element. When you add an annotation, use Member Insight for a list of options based on the statement context, and use Parameter Insight for annotation information such as the names of the elements of the annotation type, the default values, and the created values.

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.

7.4 Using Code Peek

Press Shift and point the mouse pointer at a variable or method to see its definition.

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.

7.5 Using Scroll Tips

Scroll vertically to see a window alongside the scroll bar that displays the methods that are in view and the name of the method that will next appear in view.

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.

7.6 Using InfoTips

An InfoTip, with details of the item in focus, pops up when the mouse pointer is positioned over an item.

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.

7.7 Searching Incrementally

Use the Incremental Find Forward or the Incremental Find Backward options in Search to search for an item forward or backwards.

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.

7.8 Using Shortcut Keys

Shortcut keys or accelerators are a keyboard alternative to mouse device operations and navigation. Use Preferences in Tools to go to the Shortcut Keys page to view or change existing accelerators, and to the Code Editor from the Category list to view or change accelerators for an editor.

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.

7.9 Bookmarking

Use Bookmark to see all the bookmarks in the Bookmarks window. The window also shows the line number and the name of the bookmarked method.

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 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 by pressing Ctrl+<number>.

7.10 Browsing Java Source

Browse imported classes and interfaces, member fields and methods, and local variables using Go to Declaration that takes you to the identifier source in an open Java file. JDeveloper reverse-engineers the class file for a source that is unavailable.

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

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.

7.11 Using Code Templates

In application development, use Code templates to insert pre-written code that adapts to surrounding code. Use Preferences in Tools to create and edit a code template, and view the complete list of code templates in the Code Editor Help.

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. See How to Use Code Templates.

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.

7.12 Setting Preferences for the Java Source Editor

Use Preferences in Tools to customize the behavior of the Java Source Editor, and to define the settings for the general source editing environment. Use the procedure to set the options for Code Insight as it applies to Java.

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

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

Set comment and brace-matching options for the Java source editor. Use the procedure to set the options for Java comment and brace matching in the 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.

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

Set the options to sort import statements in the Java Source Editor. Use the procedure to set the options for sorting import statements.

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.

7.12.3 How to Choose a Coding Style

Use Preferences in Tools to select a profile that decides the coding style which determines conventions for the Java Code shown in the Java Source Code editor. Use the procedure to select a coding style for the Java Source Code editor.

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.

7.12.3.1 How to Edit a Coding Style

Edit a coding style and make it available as a selectable Profile option on the Code Style page. Use the procedure to edit a code profile.

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

7.12.3.2 How to Set Up a Coding Style Using an Extension

Use an extension to create a custom coding profile that determines coding style. Use the procedure to set a default coding profile 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. See Introduction to Developing Oracle JDeveloper Extensions and How to Edit a Coding Style.

To set a default coding profile using an extension:

  1. Edit a custom 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 the following.:
    <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 the following:
    <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> 
    

7.13 Using Toolbar Options

The Java Source Editor displays Java source files and the tools to create and edit Java code. The table lists the Toolbar options that JDeveloper provides.

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

Table 7-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 (See 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.

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.

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.

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.

7.14 Using the Quick Outline Window

In the Java source editor, click Quick Outline Toolbar to see the Quick Outline window. The window provides a structural view of the available methods and fields of a class and its super classes.

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

Figure 7-3 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 7-4 Quick Outline Window

Description of Figure 7-4 follows
Description of "Figure 7-4 Quick Outline Window"

Table 7-2 lists the available icons and options.

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

Show Detail Window

Show Detail Window

Click to open the Show Detail window for the selected item in the Quick Outline window.

In the Show Detail window, click the Documentation tab to view the package containing the item.

In the Show Detail window, click the Code tab to see its definition in the source file.

7.15 Working with the Java UI Visual Editor

For Swing or Abstract Window Toolkit applications, use the Java UI Visual Editor to view and edit the visual components of the user interface.

The Java UI Visual Editor displays the visual components of a user interface in editing mode. See Editing with the Java Visual Editor .

Note:

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

7.15.1 Java Swing and AWT Components

Java Swing and AWT components are the basic building blocks, such as buttons, dialogs, and menus of an application UI. To build the UI, in the Java Visual Editor, select JavaBeans from the Components window, set the values of the component properties, and attach eventhandler code to the component events.

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.

See About Java Swing UI Components and Containers.