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
Add a menu item to a BlackBerry Device Software application
-
Import the required classes and interfaces.
import net.rim.blackberry.api.menuitem.*; import net.rim.blackberry.api.pdap.*; import net.rim.device.api.ui.*; import net.rim.device.api.ui.component.*;
-
Extend the abstract
ApplicationMenuItem class to create a menu item.
Override the
ApplicationMenuItem() constructor with an integer to
specify the position of the menu item in the menu. A higher number positions
the menu item lower in the menu.
public class SampleMenuItem extends ApplicationMenuItem { SampleMenuItem() { super(20); } } -
Implement
toString() to specify the menu item text.
public String toString() { return "Open the Contacts Demo application"; } -
Invoke
getInstance() to retrieve the application
repository.
ApplicationMenuItemRepository repository = ApplicationMenuItemRepository.getInstance();
-
Create an instance of a class to extend the
MenuItem class.
ContactsDemoMenuItem contactsDemoMenuItem = new ContactsDemoMenuItem();
-
Invoke
ApplicationMenuItemRepository.addMenuItem() to add
the menu item to the relevant
BlackBerry® device application
repository.
repository.addMenuItem(ApplicationMenuItemRepository.MENUITEM_ADDRESSCARD_VIEW, contactsDemoMenuItem);
-
Implement
run() to specify the behavior of the menu item. In
the following code sample, when a user clicks the new menu item and a
Contact object exists, the
ContactsDemo application receives the event and
invokes
ContactsDemo.enterEventDispatcher().
public Object run(Object context) { BlackBerryContact c = (BlackBerryContact)context; if ( c != null ) { new ContactsDemo().enterEventDispatcher(); } else { throw new IllegalStateException( "Context is null, expected a Contact instance"); } Dialog.alert("Viewing an email message in the email view"); return null; }
Next topic:
Changing the appearance of a menu
Previous topic: Adding a menu item to a
BlackBerry Device Software application