Help Center

Local Navigation

Create a menu item

  1. Import the following classes:
    • net.rim.blackberry.api.menuitem.ApplicationMenuItem
    • java.lang.IllegalStateException
    • net.rim.device.api.ui.component.Dialog
  2. Import the javax.microedition.pim.Contact interface.
  3. Extend the abstract ApplicationMenuItem class to define a menu item.
    public class SampleMenuItem extends ApplicationMenuItem { ... }
    
  4. Invoke ApplicationMenuItem() to specify the position of the menu item in the menu. A higher number means that the menu item appears lower in the menu.
    SampleMenuItem() {
    super(20);
    }
    
  5. Implement toString() to specify the menu item text. In the following code sample, we implement toString() to display the text "Open the Contacts Demo application" in the menu of the BlackBerry® device application.
    public String toString() {
    return "Open the Contacts Demo application";
    }
    
  6. Implement run() to specify the behaviour of the menu item. In the following code sample, if a BlackBerry device user clicks the text "Open the Contacts Demo application" in the menu of the BlackBerry device application, if a Contact exists, we allow an application called ContactsDemo to receive events by invoking ContactsDemo.enterEventDispatcher().
    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");
    }
    
    return null;
    }
    

Index


Was this information helpful? Send us your comments.