Build a Java Swing Application

You can quickly create a project in which to organize your application files, a runnable application class file, and a frame class in which you can lay out UI controls. Then work in JDeveloper's IDE to develop, compile, run, debug, and optimize your application.

Create a New Application and Project: Use the Create Application wizard to create a new application and project for your Java Swing application.

Create a Java Application: Use the Create Java Application dialog to create the skeleton files for your Java Swing application.

Develop the UI: Use the Java visual editor to easily assemble the elements of a user interface (UI) for your Java application using Swing components.

Finish Coding: To finish development, use the Java source editor, which provides functionality such as Java Code Insight and import statement assistance as you edit your source files.

Compile: Build your project utilizing the speed and advanced functionality of the Java Compiler (Javac).

Debug: Use the UI debugger to control the execution of your program while you debug it.

 

Create a New Application and Project

tell me more icon Creating a New Application and Project

The application control file is the highest level in the organizational structure. It stores information about the objects you are working with, while you are creating your application. It keeps track of your projects and the environment settings while you are developing your Java program.   [ tell me more...]





  1. Open the Create Application wizard [ File > New > General > Applications > Application] .
  2. Enter the application name MySwingApplication to follow along with the example.

    tell me more icon Application Templates

    Application templates provide you with a quick way to create the project structure for standard applications with the appropriate combination of technologies already specified. The new application created from the template appears in the Application Navigator already partitioned into tiered projects, with the associated technology scopes set in each project.   [ tell me more...]



  3. Select Java Desktop Application from the Applica tion Template list, and click Finish .

    tell me more icon In the IDE

    When you create your application based on the Java Desktop Application template, JDeveloper adds a project file named Client.jpr, so the Application Navigator should look like this:  [ tell me more...]



 

Create a Java Application

tell me more icon Creating a Java Application

You can quickly create a runnable application class file and, optionally, a frame class that you can lay out with UI controls.   [ tell me more...]





  1. In the Application Navigator, select the project you just created and open the Create Java Application dialog [ File > New > Client Tier > Swing/AWT > Java Application] .

    tell me more icon Java Application Files

    When you use the Create Java Application dialog and the subsequent Create Frame dialog, JDeveloper adds a skeleton application file and a frame file to your project, which by default are named Application1.java and Frame1.java.  [ tell me more...]



  2. Accept the default name and package for the class.
  3. Under Optional Attributes, accept the default values to create a new frame and click OK.
  4. In the Create Frame dialog, accept the default name and package for the class.
  5. Under Optional Attributes, enter My Java Swing Application as the title for the frame. Then select Menu Bar , Status Bar , and About Box to create those items for this first frame. Then click OK.

    tell me more icon In the IDE

    When you create your Java application, the Application Navigator should look like this, if all nodes are expanded:  [ tell me more...]



 

Edit with the Java Visual Editor

tell me more icon Editing with the Java Visual Editor

Using JDeveloper's Java visual editor, you can quickly and easily assemble the elements of a user interface (UI) for a Java application using Swing components. You construct the UI with JavaBeans (such as buttons, text areas, lists, dialogs, and menus) selected from the Component Palette. Then, you set the values of the component properties and attach event-handler code to the component events.   [ tell me more...]





  1. On the Swing page of the Component Palette, Components panel, select the jbutton icon JButton component and then drag and drop it in the frame in the visual editor.
  2. Resize the component by clicking to display its grab bars and then dragging the bars.
  3. Modify the position of the component by clicking and dragging it in the frame.
  4. In the Property Inspector expand the Visual section, then change the default text that displays on the button by entering Click here in the text field.

    tell me more icon Editing with the Property Inspector

    The Property Inspector lets you inspect and set component properties and attach methods to component events. Changes made in the Property Inspector are reflected in the Java visual editor and source code. The Property Inspector displays the properties of the component selections you make in the Java visual editor. The components are organized by categories, and can be easily located with the Find field on the toolbar.  [ tell me more...]



  5. With the button still selected, expand the Events section in the Property Inspector, then click the actionPerformed (...) browse button to open the Edit Property dialog. Click OK to accept jButton1_actionPerformed as the name for a generated method stub to be called from the event handler.
  6. Click save all Save All to save your work.

    tell me more icon In the IDE

    When you insert the actionPerformed event, a method is generated and the source editor automatically switches to source view so that you can edit it.  [ tell me more...]



 

Edit with the Java Source Editor

tell me more icon Editing with the Java Source Editor

Features available to you in the source editor include:   [ tell me more...]





  1. In the source editor, add sample code to the JDeveloper-generated code, to add an action-performed event for the button.
    code sample icon Use sample code
  2. To (optionally) add your own code, start typing to activate Java Code Insight.

    tell me more icon Using Java Source Editor Features

    The insert or replace mode for the editor is controlled by the Insert key on the keyboard. The default is insert mode. Press Insert to toggle between insert and replace mode.  [ tell me more...]



  3. To (optionally) add an import statement when you reference a class, select the name of the statement that appears below the ragged line.
  4. To (optionally) see Code Assist's suggested fixes for specific problems in your code, click the code assist icon Code Assist icon in the left margin.

    tell me more icon In the IDE

    If you added the code sample, the code at the bottom of the source editor should look like this:  [ tell me more...]



 

Compile and Run a Java Application

tell me more icon Compiling and Running a Java Application

You can compile several nodes in an application in any combination: the entire application, an entire project, or specific Java files.   [ tell me more...]





  1. In the Application Navigator, right-click the project and choose Ma ke Client.jpr .

    tell me more icon Choosing Between Make and Rebuild

    You can compile by choosing either the Ma ke or Re build command from the Build or context menu, or the toolbar.   [ tell me more...]



  2. Once the project is compiled, in the Application Navigator, right-click the project and choose Run .
  3. In the Choose Default Run Target dialog, browse and select Application1.java, which contains a main method, as the default run target. Then click OK.

    tell me more icon In the IDE

    The Choose Default Run Target dialog displays when you run a project for which no default run target is specified in the Run/Debug/Profile settings of the Project Properties dialog.   [ tell me more...]



 

Create a Java Class

tell me more icon Creating a Java Class

New classes can be created and added to your project after you build the initial set of project files.   [ tell me more...]





  1. In the Application Navigator, select the Client project and open the Create Java Class dialog [ File > New > General > Java Class] .
  2. Enter Class1 as the class name if you are using the sample code. Accept the package name.
  3. If working with the sample code, enter javax.swing.JDialog in the Extends field, by clicking lov icon Browse and using the Class Browser.
  4. Under Optional Attributes, deselect Con structors from Superclass , and confirm that Main Method is not selected. Then click OK.
  5. In the editor window, select the Frame1.java tab at the top, and then the Source tab at the bottom, to view the Frame1.java file in the source editor. Then add code to include this new Java class file in your application.
    code sample icon Use sample code
  6. When your changes are complete, in the Application Navigator, right-click the Client project and choose Run .

    tell me more icon In the IDE

    When you create your Java class, the Application Navigator should look like this:  [ tell me more...]



 

Debug a Java Swing Application

tell me more icon Debugging a Java Swing Application

In addition to JDeveloper's standard Java and PL/SQL debugger facilities, JDeveloper also provides support for debugging graphical user interfaces (GUIs) specifically for AWT and Swing-based client applications and applets.   [ tell me more...]





  1. Choose Run then choose UI De bug Client.jpr to start the project's default target and run the application.
  2. Choose View then choose Java then choose UI Debugger Tree , and click the snapshoy icon Snapshot icon in the UI Tree window toolbar.

    tell me more icon UI Debugger Windows

    You can use the UI Debugger features which are exposed in JDeveloper via three dockable windows: the UI Tree, the UI Outline, and the Events windows. The Events window appears the first time you track events. You can toggle all three windows by choosing View then choose Java then choose UI Debugger Outline, Tree or Events.  [ tell me more...]



  3. Right-click the client.Frame1(0) component in the UI Tree window and choose Trace Events from the context menu to open the Trace Events dialog.
  4. Select java.awt.event.MouseListener from the list and click OK.
  5. Bring the application to the front, then move the mouse into the Frame and note the generated events in the Events window.
  6. End the debugging session at any time by clicking terminate icon Terminate.

    tell me more icon In the IDE

    When you start the UI debugger, the Log window opens and displays the debugging trace.   [ tell me more...]



 

Copyright © 1997, 2009, Oracle. All rights reserved.

false ,,,,,,,,,,,,,,,