Would you like to tell us how we are doing?

You bet No thanks

Sorry about the red box, but we really need you to update your browser. Read this excellent article if you're wondering why we are no longer supporting this browser version. Go to Browse Happy for browser suggestions and how to update.

Menu sample app

The menu demo sample application demonstrates how to create a menu, then add the following customizations to it:

  • An image or icon
  • A submenu
  • A different font
  • A background color
  • A border

File name

Description

MenuDemo.java

This file defines four classes, MenuDemo, MenuDemoScreen, DemoMenuItem, and ImageMenuItem.

The MenuDemo class extends UiApplication to support a user interface and contains the following constructor and methods:

  • main (String[] args): provides the entry point to the sample application and starts the main thread using enterEventDispatcher() from UiApplication
  • MenuDemo(): constructs a new MenuDemo object, then creates an MenuDemoScreen object and displays it

The MenuDemoScreen class extends MainScreen to create the screen of the application, and implements the FieldChangeListener interface to listen for changes in the state of the fields on the screen. The MenuDemoScreen class contains the following constructor, methods and classes:

  • MenuDemoScreen(): constructs a new MenuDemoScreen object, containing several types of fields, and adds these fields to the screen manager inherited from MainScreen, as well as three DemoMenuItem objects to populate the SubMenu
  • fieldChanged(Field field, int context): implements fieldChanged(Field field, int context) of the FieldChangeListener interface. When a BlackBerry device user selects the appropriate RadioButtonField object, the ObjectChoiceField object becomes editable
  • makeMenu(Menu menu, int context): overrides MainScreen.MakeMenu(Menu menu, int context) to modify the default menu by adding menu items, sub menus, and images to menu items based on the fields that you select
  • DemoMenuItem: an inner class with no executable code that extends MenuItem to demonstrate the Sub Menu API
  • onSavePrompt(): a method from the MainScreen class overridden to return true, suppressing the save dialog before closing

The ImageMenuItem class extends MenuItem, and contains the following constructor:

  • ImageMenuItem(): class constructor, which creates a menu item with an icon using the inherited MenuItem.setIcon(Image arg0) method

This sample features the following classes:

net.rim.device.api.ui.component.Menu

This class provides a menu that is displayed when users press the Menu key. It consists of methods for populating the menu with objects (MenuItem objects, for example), and for customizing the appearance of the menu, (setFont(Font font), for example).

In the sample application, the menu is populated with MenuItem, SubMenu, and SeparatorField objects. Selecting the different options dynamically changes which objects are added to the menu, as well as how it looks.

net.rim.device.api.ui.menu.SubMenu

This class creates submenus that are added to menus. The submenu is populated by MenuItem objects, that are added to the Menu object by invoking Menu.add(Submenu submenu).

In the sample application, the submenu is added to the menu object when you select the "Menu with sub menu" option. The submenu in the sample application is populated with DemoMenuItem objects, a subclass of MenuItem created in the sample application.

net.rim.device.api.ui.MenuItem

This class creates an object that represents an abstract menu item.

The sample application uses two subclasses of the MenuItem class, DemoMenuItem and ImageMenuItem. DemoMenuItem is created as an inner class, and demonstrates the SubMenu class by populating a SubMenu with DemoMenuItem objects. ImageMenuItem demonstrates how to add an image to menu items.

Install the sample app

In the BlackBerry Java Development Environment, open the workspace that you want to add the sample application to.

In the Files pane, right-click <workspace_name>.

Click Add project to <workspace_name>.

In the Add project to <workspace_name> dialog box, browse to the menudemo folder. The default location of this folder is C:\Program Files\Research In Motion\BlackBerry JDE 5.0.0\samples\com\rim\samples\device\ui\menudemo.

Click the MenuDemo.jdp file.

Click Open.

The sample app is also available in github.

Run the sample app

In the BlackBerry Java Development Environment, in the workspace that you added the MenuDemo project to, right-click MenuDemo.

Click Build project.

On the taskbar, click Start > Applications > Research In Motion > BlackBerry JDE 5.0.0 > Device Simulator.

On the Home screen of the BlackBerry Smartphone Simulator, on the screen, click All.

Click the Menu Demo icon.