Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Build Your First BlackBerry Java App : Page 3

Learn how to use the freely available BlackBerry JDE to develop, debug, and test BlackBerry applications. Follow this demonstration to create a simple HelloWorld application that exploits some of BlackBerry's own APIs.


advertisement
Say HelloWorld
At this point, you are ready to create your HelloWorld application. Since you want your HelloWorld application to provide a graphical user interface (GUI), you need your HelloWorld class to extend the net.rim.device.api.ui.UiApplication class. (Download the code for the HelloWorld application from the zip file that accompanies this article.)

Your BlackBerry application starts like a typical J2SE application does, with a main method. If you are an experienced MIDlet programmer, you should notice something weird here. A MIDlet starts at the startApp() method, but BlackBerry applications start at main:

class HelloWorld extends net.rim.device.api.ui.UiApplication { public static void main(String[] args) { HelloWorld instance = new HelloWorld(); instance.enterEventDispatcher(); } public HelloWorld() { pushScreen(new SalutationScreen()); } }



The first thing you do in the main method is create an instance of your application by calling its constructor. The constructor uses the HelloWorld class' parent class (UiApplication) method of pushScreen to display a screen. You haven't defined the SalutationScreen class yet, but rest assured, you'll get to that. After calling the constructor, call your new instance's enterEventDispatcher method. This method allows your application to start handling various events that the BlackBerry device may send to the application (e.g., UI-centric events).

Create a new class in the HelloWorldWorkspace and HelloWorldProject, as you did before, but this time call the new class SalutationScreen.java. The SalutationScreen class is what will actually present a Hello World message to your application user. SalutationScreen.java is shown below:

import net.rim.device.api.ui.*; import net.rim.device.api.ui.component.*; import net.rim.device.api.ui.container.*; class SalutationScreen extends MainScreen { public SalutationScreen() { super(); LabelField applicationTitle = new LabelField("Hello World Title"); setTitle(applicationTitle); RichTextField helloWorldTextField = new RichTextField("Hello World!"); add(helloWorldTextField); } public boolean onClose() { Dialog.alert("Bye World!"); System.exit(0); return true; } }

The SalutationScreen Class Dissected
The SalutationScreen class extends from the net.rim.device.api.ui.container.MainScreen class, giving your simple application consistency with other native BlackBerry applications. It also provides such features as a default menu with a Close menu item for exiting out of your application, which the user also can invoke by pressing the BlackBerry Escape key.

To add a title to your simple application, create a new net.rim.device.api.ui.component.LabelField object:

LabelField applicationTitle = new LabelField("Hello World Title");

The code above contains a field with the text "Hello World Title". You can always learn more about this API call, as well as other APIs, by taking a look at the API Reference that installed with the JDE. To view the API reference, go to the Help menu and choose the API Reference option or simply press shift+F1 (see Figure 5).

 
Figure 5. Using the JDE's API Reference

After creating your LabelField GUI component, add it to the screen by using the add method you inherited from your ancestor class net.rim.device.api.ui.Screen.

You also inherit the onClose method from the net.rim.device.api.ui.Screen, which is fired when your screen (Salutation Screen) closes. In reaction to the closing event, the application uses the alert method of the net.rim.device.api.ui.component.Dialog class to display a popup on the screen stating a message of "Bye World!":

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



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap