Help Center
Local Navigation
- Creating a UI that is consistent with standard BlackBerry UIs
- BlackBerry device user input and navigation
- Screens
- Accelerometer
- Events
- Arranging UI components
- UI components
- 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 Application - 5.0
Create a menu
The MainScreen
class provides standard components of a BlackBerry® device application. It includes a default menu.
- 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 CreateMenuScreen
class, which is described in step 3, represents the custom
screen.
public class CreateMenu extends UiApplication { public static void main(String[] args) { CreateMenu theApp = new CreateMenu(); theApp.enterEventDispatcher(); } public CreateMenu() { pushScreen(new CreateMenuScreen()); } } - 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.
Invoke addMenuItem() to add a menu item to the menu that MainScreen creates.
class CreateMenuScreen extends MainScreen { public CreateMenuScreen() { setTitle("Create Menu Sample"); add(new RichTextField("Create a menu")); addMenuItem(_viewItem); } } - Create the menu item by using the MenuItem class. 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 _viewItem = new MenuItem("More Info", 110, 10) { public void run() { Dialog.inform("Display more information"); } }; - Override close() to display a dialog box when the user clicks the Close menu item. By default, the Close menu item is included in the menu that MainScreen creates. Invoke super.close() to close the application. When the user closes the dialog box, the application invokes MainScreen.close() to close the application.
public void close() { Dialog.alert("Goodbye!"); super.close(); }
Code sample: Creating a menu
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
public class CreateMenu extends UiApplication
{
public static void main(String[] args)
{
CreateMenu theApp = new CreateMenu();
theApp.enterEventDispatcher();
}
public CreateMenu()
{
pushScreen(new CreateMenuScreen());
}
}
class CreateMenuScreen extends MainScreen
{
public CreateMenuScreen()
{
setTitle("Create Menu Sample");
add(new RichTextField("Create a menu"));
addMenuItem(_viewItem);
}
private MenuItem _viewItem = new MenuItem("More Info", 110, 10)
{
public void run()
{
Dialog.inform("Display more information");
}
};
public void close()
{
Dialog.alert("Goodbye!");
super.close();
}
}
Next topic: Best practice: Implementing menus
Previous topic:
Menu items