Thursday, February 21, 2008

Developing Applications Using the JDE

Recall that BlackBerry applications that support user interaction extend net.rim.device.api.ui.UiApplication. This class provides methods for applications to register event listeners, manage threads, and manage UI components. When an application starts, the JVM invokes main(), which in turn calls enterEventDispatcher() to start handling events.

To get started developing BlackBerry applications with the JDE, you create a workspace, and subdirectories for each project. Then you create source files, build the application, and test it, much as in any IDE. Let's walk through development of a simple example.

Create a Workspace
  1. In the IDE, from the File menu, choose New workspace.

  2. Enter a name for the workspace and a pathname for the directory where it should be saved, and press OK.

    Figure 3: Create a Workspace
    Figure 3: Create a Workspace
Create a Project
  1. From the Project menu, choose Create new project.

  2. Enter a name for the project and a directory where it should be saved, and press OK.
Figure 4: Create a Project
Figure 4: Create a Project
Create the Source Files
  1. From the File menu, choose New.

  2. Give a name to the file; include the .java extension.

  3. Enter the pathname of the project directory where you want the file saved, and press OK.

    Figure 5: Create a Java Source File
    Figure 5: Create a Java Source File

  4. In the editor pane, right-click the file, and choose Insert into project.

    Figure 6: Insert a File Into a Project
    Figure 6: Insert a File Into a Project

  5. Select the project and press OK.

  6. In the editor pane, enter the code for this sample Blackberry application:

    Code Sample 1: HelloApp.java
    import net.rim.device.api.ui.*;
    import net.rim.device.api.ui.component.*;
    import net.rim.device.api.ui.container.*;
    import net.rim.device.api.system.*;

    public class HelloApp extends UiApplication {
    public static void main(String argv[]) {
    HelloApp app = new HelloApp();
    app.enterEventDispatcher();
    }

    public HelloApp() {
    pushScreen(new HelloScreen());
    }
    }

    class HelloScreen extends MainScreen {
    public HelloScreen() {
    super();
    LabelField title = new LabelField
    ("BlackBerry App", LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
    setTitle(title);
    add(new RichTextField("Welcome to Developing BlackBerry Apps Tutorial"));
    }

    public boolean onClose() {
    Dialog.alert("Visit Again!");
    System.exit(0);
    return true;
    }
    }

Once you've entered this code, you can build the project. To compile the source file, perform preverification, and package the application into a .cod file, from the Build menu choose Build all.

Figure 7: Code the Source File
Figure 7: Code the Source File (click image for full size)

The JDE will generate FirstApp.jad, FirstApp.jar, and FirstApp.cod, among other files, in the project directory you created earlier.

Now you can run the application in the simulator. In the JDE's Build menu choose Build all and run. The application will appear in the simulator. Just select it, and it will display a welcome message. When you exit, you'll see the message "Visit Again!" as in Figure 8.

Figure 8: Sample run of HelloApp in BlackBerry simulator
Figure 8: Sample run of HelloApp in BlackBerry simulator

To load the application into a real device, use the javaloader command-line tool as I described earlier:

javaloader -usb load FirstApp.cod

No comments: