Create the framework for applications with a UI
This task demonstrates how to create an instance of your application class, push your application's first screen on to the display stack, and start the event thread. You perform these actions at the beginning of nearly every
BlackBerry® device application that you create.
- Import the required classes and interfaces.
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;
- Create the class that represents your application. Extend the UiApplication class to create an application that has a UI.
public class MyApplication extends UiApplication
{
- Implement main() in your application class. This method represents the primary entry point for your application.
public static void main(String[] args)
{
- In main(), create an instance of your application class.
MyApplication myApp = new MyApplication();
- Invoke enterEventDispatcher() to start the event thread and allow your application to process events.
myApp.enterEventDispatcher();
}
- Implement the constructor for your application class.
public MyApplication()
{
- In the application constructor, invoke pushScreen() to push an instance of your application's first screen on to the display stack.
pushScreen(new MyApplicationScreen());
}
}
- Create the class that represents your application's first screen. Extend the MainScreen class to create a screen that consists of a title section, separator element, and main scrollable section.
class MyApplicationScreen extends MainScreen
{
- Implement the constructor for your screen class.
public MyApplicationScreen()
{
- In the screen constructor, perform any initial tasks to set up your screen. For example, to set a title for your screen, invoke setTitle().
setTitle("My First BlackBerry Device Application");
}
}
Note: This task defined both the application class and screen class in the same .java file. It is good practice to place these definitions in separate .java files. Each screen class that your application uses should also be defined in a separate .java file.
Code sample: Creating the framework for applications with a UI
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;
public class MyApplication extends UiApplication
{
public static void main(String[] args)
{
MyApplication myApp = new MyApplication();
myApp.enterEventDispatcher();
}
public MyApplication()
{
pushScreen(new MyApplicationScreen());
}
}
class MyApplicationScreen extends MainScreen
{
public MyApplicationScreen()
{
setTitle("My First BlackBerry Device Application");
}
}
Was this information helpful? Send us your comments.