Posted by: chathuradd on: January 28, 2009
Once you get familiar with BlackBerry JDE environment it is time to develop a small HelloWorld application for BlackBerry….
Here it goes….
If you are using Eclipse just create a new BlackBerry project..
For that go to File -> New -> Project and expand the BlackBerry folder and select BlackBerry project and click Next. Then enter any name click Finish. A new project will be created. Then create a new java file called HelloWorld and place the below code..
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.*;
/*
* BlackBerry applications that provide a user interface
* must extend UiApplication.
*/
public class HelloWorld extends UiApplication
{
public static void main(String[] args)
{
//create a new instance of the application
//and start the application on the event thread
//…
HelloWorld app = new HelloWorld();
app.enterEventDispatcher();
}
//define a constructor for your application
//…
public HelloWorld()
{
pushScreen(new HelloWorldScreen());
}
}
//create a new screen that extends MainScreen, which provides
//default standard behavior for BlackBerry applications
final class HelloWorldScreen extends MainScreen
{
public HelloWorldScreen()
{
//invoke the MainScreen constructor
super();
//add a title to the screen
LabelField title = new LabelField(“HelloWorld Sample”,LabelField.ELLIPSIS|LabelField.USE_ALL_WIDTH);
//add the text “Hello World!” to the screen
setTitle(title);
}
//override the onClose() method to display a dialog box to the user
//with “Goodbye!” when the application is closed
public boolean onClose()
{
//display a dialog box with “Goodbye!”
//…
Dialog.alert(“Good bye!!”);
System.exit(0);
return true;
}
}
When you run the application select it to run in BlackBerry simulator and the simulator will run.
Then select the HelloWorld application from the application list in BB simulator and click on it.
If the application is running perfectly you will see the word “HelloWorld” displayed as the title. If you press Esc key the Dialog box will appear saying “Good bye” and you will exit the application.
Congratulations!!! You have successfully run your first BlackBerry application. Hope this tiny application will help you to get a quick start into the BlackBerry development.