Help Center
Local Navigation
- Creating a UI that is consistent with standard BlackBerry UIs
- BlackBerry device user input and navigation
- Screens
- Accelerometer
- Events
- Command Framework API
- Arranging UI components
- UI components
- Images
- 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 SDK - 6.0
Create a pop-up menu
-
Import the required classes and interfaces.
import net.rim.device.api.command.*; import net.rim.device.api.system.*; import net.rim.device.api.ui.*; import net.rim.device.api.ui.component.*; import net.rim.device.api.ui.container.*; import net.rim.device.api.ui.menu.*; import net.rim.device.api.ui.image.*; import net.rim.device.api.util.*; import java.util.*;
-
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
MyPopUpMenuScreen class, which is described in step
3, represents the custom screen.
public class MyPopUpMenuApp extends UiApplication { public static void main(String[] args) { Mypop-upMenuApp theApp = new Mypop-upMenuApp(); theApp.enterEventDispatcher(); } public Mypop-upMenuApp() { pushScreen(new Mypop-upMenuScreen()); } } -
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.
class MyPopUpMenuScreen extends MainScreen { EmailAddressEditField emailAddress; public Mypop-upMenuScreen() { setTitle("Pop-Up Menu Demo"); } } -
In the screen constructor, specify a context menu provider. Pass a
DefaultContextMenuProvider object to
Screen.setContextMenuProvider() to enable your
screen to display a pop-up menu.
setContextMenuProvider(new DefaultContextMenuProvider());
-
In the screen constructor, create UI components that can invoke
the pop-up menu. In the following code sample, the label uses the
Field.FOCUSABLE property to allow users to highlight
the field.
LabelField labelField = new LabelField("Click to invoke pop-up menu", Field.FOCUSABLE); emailAddress = new EmailAddressEditField("Email address: ", "name@blackberry.com", 40); -
In the screen constructor, configure the UI components as command
item providers. The
DefaultContextMenuProvider object looks for fields
that are configured as command item providers, and uses them to create and
display a pop-up menu. For each component, invoke
Field.setCommandItemProvider() to configure the
field as a command item provider. The
ItemProvider class is described in step 7.
ItemProvider itemProvider = new ItemProvider(); labelField.setCommandItemProvider(itemProvider); emailAddress.setCommandItemProvider(itemProvider); -
In the custom screen, implement the
CommandItemProvider interface. In
getContext(), return the field that is configured as
a command item provider. In
getItems(), create a
Vector object to add the pop-up menu items to.
class ItemProvider implements CommandItemProvider { public Object getContext(Field field) { return field; } public Vector getItems(Field field) { } } -
In
getItems(), provide the pop-up menu items by
creating instances of the
CommandItem class. For each command item, specify
the pop-up menu text, icon, and command. In the following code sample, an
if-then-else statement is used to check which
component invoked the pop-up menu before creating the
CommandItem. The command is a proxy to an instance
of a class that extends the abstract
CommandHandler class, which is described in step 9.
Invoke
Vector.addElement() to add the pop-up menu items to
the
Vector. Return the
Vector.
CommandItem defaultCmd; Image myIcon = ImageFactory.createImage(Bitmap.getBitmapResource("my_logo.png")); if(field.equals(emailAddress)){ defaultCmd = new CommandItem(new StringProvider("Email Address"), myIcon, new Command(new DialogCommandHandler())); } else { defaultCmd = new CommandItem(new StringProvider("Label Field"), myIcon, new Command(new DialogCommandHandler())); } items.addElement(defaultCmd); return items; -
In the custom screen, create a command handler by creating a class
that extends the abstract
CommandHandler class. In
execute(), define the functionality that you want to
associate with the pop-up menu items. This command handler could be used by any
UI component on your screen (for example, full menu items, buttons, and so on).
Because
canExecute() is not implemented, this command is
always executable. In the following code sample, a dialog box appears when the
user clicks a pop-up menu item.
class DialogCommandHandler extends CommandHandler { public void execute(ReadOnlyCommandMetadata metadata, Object context) { Dialog.alert("Executing command for " + context.toString()); } }
Code sample: Creating a pop-up menu
import net.rim.device.api.command.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.menu.*;
import net.rim.device.api.ui.image.*;
import net.rim.device.api.util.*;
import java.util.*;
public class MyPopUpMenuApp extends UiApplication
{
public static void main(String[] args)
{
MyPopUpMenuApp theApp = new MyPopUpMenuApp();
theApp.enterEventDispatcher();
}
public MyPopUpMenuApp()
{
pushScreen(new MyPopUpMenuScreen());
}
}
class MyPopUpMenuScreen extends MainScreen
{
EmailAddressEditField emailAddress;
public MyPopUpMenuScreen()
{
setTitle("Pop-Up Menu Demo");
setContextMenuProvider(new DefaultContextMenuProvider());
LabelField labelField = new LabelField("Click to invoke pop-up menu", Field.FOCUSABLE);
emailAddress = new EmailAddressEditField("Email address: ", "name@blackberry.com", 40);
ItemProvider itemProvider = new ItemProvider();
labelField.setCommandItemProvider(itemProvider);
emailAddress.setCommandItemProvider(itemProvider);
add(labelField);
add(emailAddress);
}
/* To override the default functionality that prompts the user to save changes before the application closes,
* override the MainScreen.onSavePrompt() method. In the following code sample, the return value is true which
* indicates that the application does not prompt the user before closing.
*/
protected boolean onSavePrompt()
{
return true;
}
class ItemProvider implements CommandItemProvider
{
public Object getContext(Field field)
{
return field;
}
public Vector getItems(Field field)
{
Vector items = new Vector();
CommandItem defaultCmd;
Image myIcon = ImageFactory.createImage(Bitmap.getBitmapResource("my_logo.png"));
if(field.equals(emailAddress)){
defaultCmd = new CommandItem(new StringProvider("Email Address"), myIcon, new Command(new DialogCommandHandler()));
}
else{
defaultCmd = new CommandItem(new StringProvider("Label Field"), myIcon, new Command(new DialogCommandHandler()));
}
items.addElement(defaultCmd);
return items;
}
}
class DialogCommandHandler extends CommandHandler
{
public void execute(ReadOnlyCommandMetadata metadata, Object context)
{
Dialog.alert("Executing command for " + context.toString());
}
}
}
Previous topic: Creating a pop-up menu