Lesson 2: Display a BlackBerry application screen

In this lesson, you create a class with a main method that opens a HomeScreen, which contains a title and a status message.

  1. In the workspace view, right-click the project and choose Create New File In Project.
  2. In the Source File Name box, type myapp\Application.java to create a file named Application.java that is part of the myapp package.

    Click OK to create the file. The Application class appears in the JDE window.

  3. Define the Application class. No imports are needed for this class. Add a constructor and a main method so that your Application class is defined as follows:
    class Application extends net.rim.device.api.ui.UiApplication {
        public static void main( String[] args )
        {
            Application instance = new Application();
            instance.enterEventDispatcher();
        }    
        Application() { 
            pushScreen( new HomeScreen() );
        }
    }
  4. Add the HomeScreen class to your project.
    1. In the workspace view, right-click the project and choose Create New File In Project.
    2. In the Source File Name box, type myapp\HomeScreen.java.
    3. Click OK to create the file.

      The HomeScreen class appears in the JDE window.

  5. Define the HomeScreen class so that it displays a title and a Status message.
    package myapp;
    import net.rim.device.api.ui.*;
    import net.rim.device.api.ui.component.*;
    import net.rim.device.api.ui.container.*;
    import java.util.*;
    
    class HomeScreen extends MainScreen {
    
        HomeScreen() {
            
            // Set the window title
            LabelField applicationTitle = new LabelField("Hello BlackBerry");
            setTitle(applicationTitle);
            
            // Add a label to show application status
            _statusLabel = new LabelField( "Status: Started" );
            add( _statusLabel );
        }
        private LabelField _statusLabel;
    }

    The _statusLabel is defined as a class variable so that it can be accessed from other parts of the application later.

  6. Right-click the project and choose Build. Ensure that it compiles without errors.
  7. Press F5 to run the application in the device simulator.

    The BlackBerry simulator launches in a separate window.

  8. On the simulator, navigate to the Applications window and select the HelloBlackBerry application.
  9. Start the application.

    A window appears showing the title bar Hello BlackBerry and the status line Status: started.

  10. From the JDE Debug menu choose Stop Debugging.

    The simulator terminates.