
Hello All;
I wish you receive this post in the best of everything.
This is the 3rd part of the BlackBerry Application Development Tutorial series. First of all, accept my apology for posting this post so late. I was busy with some other projects, and hence couldn't find enough time to compose it. I delayed this task for this weekend, and now it's ready for you.
Note: This tutorial is a part of a series of my tutorials on BlackBerry application development; so far we have discussed the following.
The pre-request of this tutorial is
BlackBerry Application Development Tutorial (Part-2) for setting your system to start bBlackBerryapplication development.
Before start, I’m assuming that you have configure the Sun JDK, Eclipse SDK, BlackBerry JDE Plug-in for Eclipse and BlackBerry JDE Component Packs successfully on your system … is that? Kindly confirm again …
Orite, its time to move forward. Bring a cup of coffee, some cookies, play some light song in your favorite music player …. I think some instrumental number will good …
Hmm now you are ready to get start .. .good … start eclipse and make a new workspace for your blackberry projects.

Alright, next is to create a new project. As you have already installed the JDE plug-in for Eclipse, you will see an entry of “BlackBerry Project” in the Create New Project window.

Click next. Chose your project name, but for god sake, don’t use HelloWorld type words... its time to say good by to Hello World … do something new, dude … ummm use “
WelcomeBlackBerry.” Hmm, that’s nice …
Chose default location; it will save project in your workspace folder… Click next and finish.
It's better... in fact, I recommend configuring your project or workspace. Chose configure blackberry workspace and set options like vendor, version, etc.

In install component, choose the BlackBerry JDE Component Packs option.
Great. You have set your workspace for BlackBerry applications. Take a sip of coffee and take a bite of cookie. Now its time to learn few concepts … keep your coffee mug in your hand and concentrate on the following paragraph. Keep taking sips of coffee.
PUSH/POP of Screens
Every software application has different pages to display different information and components for user interactions like buttons, text fields etc. In BlackBerry applications, we will call them as screens. Mean we will design and develop different screens and will link them via different events like clicking a button will hide one page(screen) and will show other page (screen).
This show/hide phenomenon in blackberry’s program is called pus and pop of a screen.
Push mean displaying a screen and
Pop means hide it. Its means when we want to display a screen, we first pop top screen and then push that screen we want to show. Hence application is behaving like a stack where we are push and pop screen and maintain a flow of screen as per events triggered by user.
Got the idea? No!!! don’t worry. Read the above paragraph again and when clear move to next one. It’s quite simple han …
Your First program for BlackBerry
Now create a new package blackberry and create new Class. Name the new class “FirstBlackBerryClass”
Every class that use user interaction controls must implement a class UiApplication (net.rim.device.api.ui.UiApplication). hey don’t smile .. I know the same class is available in iPhone sdk too, for same purpose :)
Ok, you have created a class, have implement UiApplication. Now make a main() method to have entry point of the our application. This main method is same as traditional java classes … yes, you got it. Its our public static void main (String[] args)
at this point our class should look like
[code language="java"]
package com.iainteractive.blackberry;
import net.rim.device.api.ui.UiApplication;
public FirstBlackBerryClass extends UiApplication
{
public static void main(String[] args) { }
public FirstBlackBerryClass() { }
}
[/code]
Now in main method, creates an instance/object of your class and calls enterEventDispatcher(), inherited from UiApplication class. This method starts event handling and executes all drawing and event-handling code. Now your class should look like
[code language="java"]
com.iainteractive.blackberry;
import net.rim.device.api.ui.UiApplication;
public FirstBlackBerryClass extends UiApplication {
public static void main(String[] args) {
WelcomeBlackBerry firstApplication  = new WelcomeBlackBerry();
firstApplication.enterEventDispatcher();
}
public FirstBlackBerryClass(){ }
}
[/code]
Our class is read to execute, means at very very basic level you can say you have created your first application.Though it will do nothing. Now its time to add some screen. When our program will run, we should make sure that we have push a screen so that when application start execution, user can see something over his device. To do so, we will first create a screen and then will link it to our main class .i.e. FirstBlackBerryClass.
Creating a Screen
Blackberry API provides a class name MainScreen that provides features common to standard RIM device applications like a title section, a separator element, and a main scrollable section (actually a single vertical field manager used to maintain a list of fields). So every class that behave like screen or in other words, every screen must extends MainScreen class to get access to different screen sections. So our next task is to develop a screen class. Add new class in package, name it as FirstScreen
[code language="java"]
package com.iainteractive.blackberry;
import net.rim.device.api.ui.container.MainScreen;
final class FirstScreen extends MainScreen {
FirstScreen(){
}
}
[/code]
Now we will add some element to display. First important thing is title for your application. For this, use LabelField class object and set some string as title. Then set this label as application’s title.
[code language="java"]
LabelField appTitle = new LabelField("HelloWorld Sample", LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
this.setTitle(appTitle);
[/code]
now to add some text over the screen, use RichTextField class and pass some string to display.
[code language="java"]
RichTextField  greeting = new RichTextField("its running… great");
[/code]
Now add it to screen by add() method of MainScreen class.
[code language="java"]
this.add(greeting);
[/code]
now our FirstScreen class will look like that
[code language="java"]
final class FirstScreen extends MainScreen {
FirstScreen(){
LabelField appTitle = new LabelField("HelloWorld Sample", LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
this.setTitle(appTitle);
RichTextField  greeting = new RichTextField("its running… great “);
this.add(greeting);
}
}
[/code]
Our screen is ready. Now time is to link it with our main program .i.e. FirstBlackBerryClass. We have learned that to show a screen in blackberry application, we have to push it, so to show our FirstScreen, we will push it to in our FirstBlackBerryClass class.
For this we will use pushScreen method of UiApplication class that takes a reference of a Screen object. We will call this method in constructor of our FirstBlackBerryClass class so that as soon as our application run, it push our screen to display on user’s device.
Now go back to FirstBlackBerryClass class’s constructor and the following line there.
[code language="java"]
pushScreen(new FirstScreen());
[/code]
its done … we have done .. now our final code should look like that
//======================
[code language="java"]
package com.iainteractive.blackberry;
import net.rim.device.api.ui.UiApplication;
public class FirstBlackBerryClass extends UiApplication {
public static void main(String[] args) {
FirstBlackBerryClass firstApplication  = new FirstBlackBerryClass();
firstApplication.enterEventDispatcher();
}
public FirstBlackBerryClass(){
pushScreen(new FirstScreen());
}
}
//======================
package com.iainteractive.blackberry;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.RichTextField;
final class FirstScreen extends MainScreen {
FirstScreen(){
LabelField appTitle = new LabelField("HelloWorld Sample", LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
this.setTitle(appTitle);
RichTextField  greeting = new RichTextField("its running… great“);
this.add(greeting);
}
}
//======================
[/code]
Test Your Application in the Simulator
for testing application in device simulator, do the following steps, click on Run or the green shortcut icon on the toolbar.

when your simulator starts, find and start your application from Downloads folder.
When you run the application you should see
its running… great message.
To exit the simulator, just close its window.
Comments (13)
By profession, I am also a software engineer.
I have a query:
I want to display LabelField at the bottom of screen in such a way that it automaticaly renders at the bottom of each screen of any size.
Plz resolve this issue of mine.
Waiting for reply....
Thanks,
Anish
I have enjoyed this tutorial... I am now trying to expand this application to include three Numeric fields for user input, and buttons to calculate the total and the 3 fields, and to calculate the average of the 3 fields. I would appreciate any assistance you may wish to provide. Thanks and keep up the good work.
Regards
Alex
Pls dont stop n keep posting such helping stuffs...
Am new to Java and haven't tested the program yet, but isn't there a mistake under the section "Your First Program for Blackberry"?
package com.iainteractive.blackberry;
import net.rim.device.api.ui.UiApplication;
public FirstBlackBerryClass extends UiApplication {
public static void main(String[] args) {
WelcomeBlackBerry firstApplication = new WelcomeBlackBerry();
firstApplication.enterEventDispatcher();
}
Shouldn't the class instantiation be:
FirstBlackBerryClass firstApplication = new FirstBlackBerryClass();
It seems to be corrected in the full code for the program but it is misleading at the top.
Leave a Reply