Pages

Tuesday, April 7, 2009

Blackbery development using NetBeans and JDE


Blackbery development using NetBeans and JDE



It is very difficult to work with the JDE provided by blackberry, since it lacks a lot of specialties of the present decades IDE like it does not allow you to move from the method call to method definition. Indeed I realize that it is not more than a simple text editor except that to build, package and run the BlackBerry applications we need it. It does not provide you facility ‘CTRL + Space’ as to NetBeans and Eclipse. These are too simple features which are not there in the JDE. Many other sophisticated features are there which are not there in the JDE.
So to develop Blackberry applications, I came with another approach after some googling and help of some friends.
I am using NetBeans and JDE both – Development is done in NetBeans and Building, Running and packaging of the project is done in JDE. I used following environment/softwares for this:
1. Java 1.6
2. NetBeans
3. BlackBerry 4.5
I am writing this blog after being inspired from following tutorial,
http://developerlife.com/tutorials/?p=478
In this article I assume that you have successfully downloaded and install the NetBeans IDE and JDE for blackberry. If you don’t please visit the above link which provides all steps for the installation.
Following are steps for coding in NetBeans and Building and running project in JDE:
1. Create a J2ME project in Netbeans. Steps to create project in NetBeans are as follows:
a) Goto File > New Project
b) Select Java ME in the new window as shown in the Figure 1.

Figure 1

c) Select Mobile application from the right side of the window as shown in Figure 2.
Figure 2

d) Click on next it will display Figure 3
Figure 3



e) Enter project name and the location of project in this screen.
f) Since we don’t want to create a J2ME MIDLet application so uncheck “Create Hello MIDLet” checkbox. Click on next.
g) In ‘Default Platform Selection’ change Device profile MIDP 2.0. Leave all other settings as it is as shown in Figure 4. Now click on finish.
Figure 4

h) Right click on the project, open properties window and add rim api jar at the build path.
2. Now open JDE.
a. Click on File > New. It will open screen as shown in figure 5.
Figure 5

b. Enter workspace location and name.
c. When you create a workspace for the BlackBerry, It will not create a folder instead it creates a *.jdw file for the workspace with name you entered in the Workspace name and at the location you entered at Location field.
d. Now you will have to create a project in this workspace. Right click on the workspace shown in the left pane as shown in figure 6 and click on the ‘Create new Project in HelloBlackBerry…..’ item.
Figure 6

e. Enter the project name in the new window. I entered project name as HelloBlackBerry.
Figure 7


3. Adding Source files In JDE: This is a major limitation of the JDE. For each new file you have to add that file in the project after creating it. Because of this I started approach as follows:
First creating a source file in the NetBeans. This is helpful since we can use NetBeans features to for coding.
In second step, after coding a source java file, adding new file into the JDE.
So let us start first by creating a simple Hello Blackberry application. Since yet I am not familiar a lot with Blackberry programming, so I am starting with familiar Hello programming.
a) I have created a package com.softneel.hello and a simple java class HelloBlackBerry in the NetBeans.
b) Extend this class with UiApplication – In my knowledge UiApplication is the base class for all ui applications. It maintains a stack of screens. It pushes those screens one by one at top of the stack and pops up those screens one by one.
For more details please refer to BlackBerry docs at following location:
http://www.blackberry.com/developers/docs/4.7.0api/
c) In the same package create a new class HelloScreen. Extend this class with MainScreen class of the BlackBerry
d) In HelloScreen class create a public constructor.
e)In this constructor, instantiate a LabelField object and set it as title of the screen.
f) Now add a RichFieldText instance to the screen.
g) Now create constructor of the HelloBlackBerry class.
h) Push an instance of the HelloScreen class to the stack.
i) In the main method of the HellBlackberry class, create an instance of the class and call enterEventDispatcher method on this instance. This method is used to set the calling thread as event-dispatching thread.
Following is code for the two classes:


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package com.softneel.hello;

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.MainScreen;

/**
*
* @author Suresh Chandra Pal
*/
public class HelloBlackBerry extends UiApplication {
public static void main(String[] args) {
HelloBlackBerry theApp = new HelloBlackBerry();
theApp.enterEventDispatcher();
}
public HelloBlackBerry() {
pushScreen(new HelloScreen());
}

}

class HelloScreen extends MainScreen {
public HelloScreen() {
super();
LabelField title = new LabelField("Hello Blackberry Sample", LabelField.ELLIPSIS
| LabelField.USE_ALL_WIDTH);
setTitle(title);
add(new RichTextField("Hello Blackberry!"));

}

public boolean onClose() {
Dialog.alert("See you later!");
System.exit(0);
return true;
}


}


j) Now we are ready with the coding part. To build, run this project we need to go to JDE.
1. In the JDE we need to add each of the source files.
2. Right click on the project and click on the “Add file to project”. It will open a window.
3. Select the java files created in the netbeans, as shown in figure 8.
Figure 8

4. It will open the selected files.
5. Now select Build > Build All and Run.
6. Sometimes it may give following error:
I/O Error: Cannot run program "javac":
This is the case when java bin path is not set in System environment variable.
Set the path upto JAVA_HOME > Bin directory.
7. Now you can see the application running on Simulator.