Help Center
Local Navigation
- Creating a UI that is consistent with standard BlackBerry UIs
- BlackBerry device user input and navigation
- Screens
- Accelerometer
- Events
- Command Framework API
- Arranging UI components
- UI components
- Images
- Menu items
- Custom fonts
- Spelling checker
- Related resources
- Glossary
- Provide feedback
- Document revision history
- Legal notice
BlackBerry Manuals & Help
>
Developer Documentation
>
Java Development Guides and API Reference
>
Development Guide
UI and Navigation - BlackBerry Java SDK - 6.0
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.
- 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.*;
- 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()); } } - 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")); } } - 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"); } }; - 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
Previous topic:
Best practice: Implementing submenus