Create a submenu

You can create a submenu for your BlackBerry® device application. A submenu appears when a user selects a menu item that has a submenu associated with it.
  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 CreateSubmenuScreen class, which is described in step 3, represents the custom screen.
    public class CreateSubmenu extends UiApplication 
    {
        public static void main(String[] args)
        {
            CreateSubmenu theApp = new CreateSubmenu();
            theApp.enterEventDispatcher();
        }
        public CreateSubmenu()
        {
            pushScreen(new CreateSubmenuScreen());
        }
    }
  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.
    class CreateSubmenuScreen extends MainScreen 
    {
         public CreateSubmenuScreen()
         {
            setTitle("Create Submenu Sample");
            add(new RichTextField("Create a submenu"));
         }
    }
  4. Create the submenu items by using the MenuItem class. For each submenu item, 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 _status1 = new MenuItem("Available", 100, 1)
    {
        public void run()
        {
             Dialog.inform("I'm available");
        }
    };
    private MenuItem _status2 = new MenuItem("Unavailable", 200, 2)
    {
        public void run()
        {
             Dialog.inform("I'm unavailable");
        }
    };
  5. Override makeMenu() to create the menu for the application. Create the submenu by using the SubMenu class. Invoke add() to add the submenu items to the submenu. Invoke super.makeMenu() to create the menu.
    protected void makeMenu( Menu menu, int instance )
    {
        SubMenu statusSubMenu = new SubMenu(null,"My Status",300,3);
        statusSubMenu.add(_status1);
        statusSubMenu.add(_status2);
        menu.add(statusSubMenu);
        super.makeMenu(menu, instance);
    };

Code sample: Creating a submenu

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

public class CreateSubmenu extends UiApplication
{
     public static void main(String[] args)
     {
         CreateSubmenu theApp = new CreateSubmenu();
         theApp.enterEventDispatcher();
     }
     public CreateSubmenu()
     {
         pushScreen(new CreateSubmenuScreen());
     }
} 
class CreateSubmenuScreen extends MainScreen
{
    public CreateSubmenuScreen()
    {
      setTitle("Create Submenu Sample");
      add(new RichTextField("Create a submenu"));
    }
    private MenuItem _status1 = new MenuItem("Available", 100, 1)
    {
        public void run()
        {
            Dialog.inform("I'm available");
        }
    };
    private MenuItem _status2 = new MenuItem("Unavailable", 200, 2)
    {
        public void run()
        {
            Dialog.inform("I'm unavailable");
        }
    };
    protected void makeMenu( Menu menu, int instance )
    {
        SubMenu statusSubMenu = new SubMenu(null,"My Status",300,3);
        statusSubMenu.add(_status1);
        statusSubMenu.add(_status2);
        menu.add(statusSubMenu);
        super.makeMenu(menu, instance);
    };
}
Next topic: Pop-up menus

Was this information helpful? Send us your comments.