Troubleshooting guide
36
BlackBerry Java Development Environment Development Guide
Adding menu items to BlackBerry applications
The Application Menu Item API, in the net.rim.blackberry.api.menuitem package, lets you add menu items
to BlackBerry® applications. The
ApplicationMenuItemRepository class lets you add or remove menu items
from BlackBerry applications.
Create a menu item
Register a menu item
Task Steps
Define a menu item. > Extend the abstract ApplicationMenuItem class.
public class SampleMenuItem extends ApplicationMenuItem { ... }
Specify the position of the menu
item in the menu.
A higher number means that the menu item appears lower in the menu.
>Invoke ApplicationMenuItem().
SampleMenuItem() {
super(20);
}
Specify the menu item text. > Implement toString().
public String toString() {
return "Open the Contacts Demo application";
}
Specify the behavior of the menu
item.
> Implement run().
public Object run(Object context) {
Contact c = (Contact)context; // An error occurs if this does not work.
if ( c ! null ) {
new ContactsDemo().enterEventDispatcher();
} else {
throw new IllegalStateException( "Context is null, expected a Contact
instance");
}
Dialog.alert("Viewing a message in the messaging view");
return null;
}
Task Steps
Retrieve the application menu item
repository.
>Invoke ApplicationMenuItemRepository.getInstance().
ApplicationMenuItemRepository repository =
ApplicationMenuItemRepository.getInstance();
Create your application menu item. > Invoke the constructor.
TestApplicationMenuItem tami = new TestApplicationMenuItem();
Add the menu item to the repository. >Invoke addMenuItem().
repository.addMenuItem(ApplicationMenuItemRepository.MENUITEM_ADDRESSC
ARD_VIEW, tami);