Create a menu

The MainScreen class provides standard components of a BlackBerry® device application. It includes a default menu.
  1. Import the required classes and interfaces.
    import net.rim.device.api.ui.*;
    import net.rim.device.api.ui.component.*;
    import net.rim.device.api.ui.container.*;
  2. Create the application framework by extending the UiApplication class. In main(), create an instance of the new class and invoke enterEventDispatcher() to enable the application to receive events. In the application constructor, invoke pushScreen() to display the custom screen for the application. The CreateMenuScreen class, which is described in step 3, represents the custom screen.
    public class CreateMenu extends UiApplication 
    {
       public static void main(String[] args)
       {
          CreateMenu theApp = new CreateMenu();
          theApp.enterEventDispatcher();
       }
       public CreateMenu()
       {
          pushScreen(new CreateMenuScreen());
       }
    }
  3. Create the custom screen for the application by extending the MainScreen class. In the screen constructor, invoke setTitle() to specify the title for the screen. Invoke add() to add a text field to the screen. Invoke addMenuItem() to add a menu item to the menu that MainScreen creates.
    class CreateMenuScreen extends MainScreen 
    {
       public CreateMenuScreen()
       {
          setTitle("Create Menu Sample");
          add(new RichTextField("Create a menu"));
          addMenuItem(_viewItem);
       }
    }
  4. Create the menu item by using the MenuItem class. Override run() to specify the action that occurs when the user clicks the menu item. When the user clicks the menu item, the application invokes Menu.run().
    private MenuItem _viewItem = new MenuItem("More Info", 110, 10)
    {
       public void run() 
       {
          Dialog.inform("Display more information");
       }
    };
  5. Override close() to display a dialog box when the user clicks the Close menu item. By default, the Close menu item is included in the menu that MainScreen creates. Invoke super.close() to close the application. When the user closes the dialog box, the application invokes MainScreen.close() to close the application.
    public void close()
    {
        Dialog.alert("Goodbye!");
        super.close();
    }

Code sample: Creating a menu

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;

public class CreateMenu extends UiApplication
{
     public static void main(String[] args)
     {
         CreateMenu theApp = new CreateMenu();
         theApp.enterEventDispatcher();
     }
     public CreateMenu()
     {
         pushScreen(new CreateMenuScreen());
     }
} 
class CreateMenuScreen extends MainScreen
{
    public CreateMenuScreen()
    {
      setTitle("Create Menu Sample");
      add(new RichTextField("Create a menu"));
      addMenuItem(_viewItem);
    }
    private MenuItem _viewItem = new MenuItem("More Info", 110, 10)
    {
        public void run()
        {
            Dialog.inform("Display more information");
        }
    };
    public void close()
    {
        Dialog.alert("Goodbye!");     
        super.close();
    }   
}
Previous topic: Menu items

Was this information helpful? Send us your comments.