Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Control System Menu Items

by BlackBerry Development Advisor on ‎09-14-2010 03:14 PM - edited on ‎09-20-2010 05:12 PM by Retired (4,675 Views)



This article applies to the following:

  • BlackBerry® devices based on Java® technology



The BlackBerry® APIs provides different types of fields for BlackBerry device applications (for example, EditField, GaugeField, RichTextField, and ChoiceField). Each field type has its own set of standard commands associated with it. For example, an EditField should allow the user to copy the text if it is highlighted. If text has been copied, then the user should be able to paste that content into the selected field, but only if that field is editable. The menu items Copy and Paste are called context menu items. Applications also have MenuItems that appear in every application, such as Close.


How to remove the Close or Hide application MenuItems


A context menu provides different context menu items, depending on the type of field object and the current execution state. Each UI Field object also provides different menu items. For example, if the field in focus is an EditField, it provides Cut, Copy, and Paste menu items (if an object is on the clipboard). A RichTextField only provides Copy (because it is not editable). All selectable fields provide Select and Cancel Selection items.


By default, a Close menu item, and possibly a Hide menu item, is added to the menu of a MainScreen or any other screen that uses the DEFAULT_CLOSE and DEFAULT_MENU styles. The Hide menu item appears on BlackBerry devices with BlackBerry® Device Software 4.0 Service Pack 2 and earlier. It is possible for your screen to prevent these menu items from being displayed in your menu. To do so, you can override the makeMenu method of the Screen class. The following code sample creates a menu that contains the default menu items (for example, Copy, Paste, Change Option) for the field that has focus, but does not display the Close or Hide menu items. For more examples, please see the sample applications included with the BlackBerry® Java® Development Environment.


Note: It is not possible to remove the Close menu item from a MIDlet.



protected void makeMenu(Menu menu, int instance)
	//The following will add the menu items associated with the current field
	//with focus (Copy, Paste, Change Option, etc...) to the top of the menu.
	Field focus = 
if(focus != null) { ContextMenu contextMenu = focus.getContextMenu(); if(!contextMenu.isEmpty()) { menu.add(contextMenu); menu.addSeparator(); } } //If we were to call "super.makeMenu(menu, instance);" here
        //the default menu //items would be added (Close, Hide, etc...). }


Control the display of Copy, Paste and other context specific items


Applications can use Context menus to populate the current menu with these commands automatically. Each application supports a single context menu. Each time the net.rim.device.api.ui.Field.getContextMenu() method is invoked on a field in the application, the context menu is reset with the menu items appropriate for that field. The context menu contents are set until the next time getContextMenu() is invoked.




The following is a code sample that updates the context menu by calling the getContextMenu() method each time a different field is in focus:



protected void makeMenu(Menu menu) {
   Field focus = UiApplication.getUiApplication().
   if(focus != null) {
       ContextMenu contextMenu = focus.getContextMenu(); 
       if (contextMenu !=null) {


This method updates the context menu. The getLeafFieldWithFocus() method returns the field that is currently in focus in the handheld application. The getContextMenu() method is then called to retrieve the context menu specific to the field in focus. The context menu is added to the application's main menu.

Users Online
Currently online: 15 members 2,397 guests
Please welcome our newest community members: