Click here to Skip to main content
14,028,254 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


12 bookmarked
Posted 11 Sep 2011
Licenced CPOL

Creating User Interfaces in BlackBerry Applications

, 11 Sep 2011
Rate this:
Please Sign up or sign in to vote.
This article gives an introduction to creating user interfaces in BlackBerry applications
Sample Image - maximum width is 600 pixelsSample Image - maximum width is 600 pixels


BlackBerry Java Development Environment (JDE) is used to develop applications that run on BlackBerry handheld devices.

There are two sets of APIs for creating user interfaces:

  • BlackBerry UI APIs

The MIDP UI APIs are used when you want to develop applications to run on MIDP compliant devices while BlackBerry UI APIs are used when you want to develop applications to specifically run on BlackBerry handheld devices. When a BlackBerry Java source code is compiled, it generates a .cod file. This .cod file is copied onto the device and executed by the virtual machine (vm) of the device.

In this article, I have focused on developing applications using BlackBerry UI APIs. At the end, I have created a simple application for demonstration of the basic concepts.



The main element of the user interface in a BlackBerry application is the Screen class. Only one screen can be displayed at a time. Screen objects are stored in a stack. A screen is pushed on top of the stack in order to be displayed and popped off the stack in order to be closed.

User interface components are represented by fields. All UI components are present in the net.rim.device.api.ui.component package.

The commonly used fields are:

  • Label field - This is an instance of the LabelField class and is used to display static text.
  • Text field - The TextField class is used to create an editable field in which a user can enter a value.

  • Button field - The ButtonField class is used to create a clickable field for performing an action.

  • Numeric choice field - The NumericChoice class is used to allow users to select a numeric value from a range of numbers.

  • Object choice field - The ObjectChoice class is used to allow users to select any object from a list of objects.

  • Checkbox field - The CheckboxField class is used to allow a user to select one of two choices.

  • RadioButton field - The RadioButtonField class is used to allow a user to select a single value from multiple options.

  • Gauge field - The GaugeField class is used to display a progress bar.


Layout of components on the screen can be managed by using Layout Managers. There are four layout manager classes as follows:

  • VerticalFieldManager
  • HoriziontalFieldManager
  • FlowFieldManager
  • DialogFieldManager

The following code can be used to create a vertical layout and add items to it.

VerticalFieldManager manager=new VerticalFieldManager(Manager.VERTICAL_SCROLL);


Menus can be created for performing actions. The MenuItem class can be used to create menus as follows:

private MenuItem viewItem = new MenuItem("Show Message", 100, 10)
    public void run()
        Dialog.inform("Welcome to BlackBerry JDE");

The makeMenu() function of the Screen class is overridden to add menuitems to the screen.

protected void makeMenu(Menu menu,int n)


Events can be handled by implementing the FieldChangeListener interface and fieldChanged method. For example:

class MyClass extends MainScreen implements FieldChangeListener
public void fieldChanged(Field field,int context)

Changes to fields can be monitored by calling setChangeListener method of the field as follows:


Using the Code

The following program demonstrates the use of different fields.

// Importing the required packages.

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;

class UIExample extends UiApplication	// Extending from UIApplication class.
    public static void main(String args[])	// main function.
        new UIExample().enterEventDispatcher();	// Start event handling process
    UIExample()	// Constructor.
        pushScreen(new MyClass());	// Push screen on top of the stack
class MyClass extends MainScreen implements FieldChangeListener	// User defined 
							// screen class.
    // Declaring fields.

    LabelField lfield;
    TextField tfield;
    ButtonField bField;
    NumericChoiceField nfield;
    ObjectChoiceField ofield;
    CheckboxField chkfield;
    RadioButtonField rfield1,rfield2,rfield3;
    GaugeField gfield;

    // Declaring Close Menu.

    private MenuItem closeItem = new MenuItem("Close", 100, 10)
        public void run()

    // Adding menu to screen.

    protected void makeMenu(Menu menu,int n)

    public MyClass()	// Constructor.

        // Initializing fields and creating user interface

        VerticalFieldManager manager=new VerticalFieldManager
        	(Manager.VERTICAL_SCROLL);	// Layout Manager
        lfield=new LabelField("Enter a value: ");	// LabelField
        tfield=new TextField();			// TextField
        bField=new ButtonField("OK");		// ButtonField
        nfield=new NumericChoiceField("Select a number",1,20,1);//NumericChoiceField
        String[] objects={"A","B","C","D","E","F","G","H","I","J"};// List of objects for 
							//the ObjectChoiceField
        ofield=new ObjectChoiceField("Select a value",objects);	// ObjectChoiceField
        chkfield=new CheckboxField("Select",false);		// CheckboxField
        RadioButtonGroup group=new RadioButtonGroup();		// RadioButtonGroup to 
							// select a single option
        rfield1=new RadioButtonField("Option 1",group,true);	// RadioButtonField
        rfield2=new RadioButtonField("Option 2",group,false);	// RadioButtonField
        rfield3=new RadioButtonField("Option 3",group,false);	// RadioButtonField
        gfield=new GaugeField("Select a value: ",1,100,1,
							// GaugeField
    public void fieldChanged(Field field,int context)	// Handling events.
    		// The Dialog.inform method is used to display an alert message.
            Dialog.inform("You have clicked the button");
            int n=nfield.getSelectedValue();
            Dialog.inform("You have selected "+Integer.toString(n));
            String s=ofield.getChoice(ofield.getSelectedIndex()).toString();
            Dialog.inform("You have selected "+s);
            boolean b=chkfield.getChecked();		// Check Status of checkbox.
            Dialog.inform("Option "+(b?"selected":"unselected"));
            if(rfield1.isSelected())			// Check RadioButton status.
                Dialog.inform("Option 1 selected");
            if(rfield2.isSelected())			// Check RadioButton status.
                Dialog.inform("Option 2 selected");
            if(rfield3.isSelected())			// Check RadioButton status.
                Dialog.inform("Option 3 selected");
            int value=gfield.getValue();		// Get value of Gauge
            Dialog.inform("You have selected "+value+" value");

Points of Interest

When the project is executed in the BlackBerry JDE, it is opened in the BlackBerry Simulator and can be selected from the downloads section.

I hope this article will provide users with the basic information to start developing for the BlackBerry handheld devices.


  • 10th September, 2011: Initial version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Azim Zahir
Instructor / Trainer NIIT, India
India India
I am a trainer by profession. Currently I am working with NIIT (Mumbai, India) as a Senior Faculty. I enjoy programming as a hobby. My favorite technologies are Flash, Flex and Silverlight.

Of late I have developed keen interest in WPF and Windows Mobile programming.

Apart from computers, my favorite pastime is bicycling.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Sandeep V Rao21-Jun-13 1:45
professionalSandeep V Rao21-Jun-13 1:45 
GeneralRe: My vote of 5 Pin
Azim Zahir4-Sep-13 17:18
memberAzim Zahir4-Sep-13 17:18 
QuestionCreating User Interfaces in BlackBerry Applications Pin
m.usmankhan9031-Jan-13 22:56
memberm.usmankhan9031-Jan-13 22:56 
AnswerRe: Creating User Interfaces in BlackBerry Applications Pin
Azim Zahir28-Mar-13 0:31
memberAzim Zahir28-Mar-13 0:31 
GeneralMy vote of 5 Pin
Kanasz Robert23-Sep-12 3:14
memberKanasz Robert23-Sep-12 3:14 
GeneralRe: My vote of 5 Pin
Azim Zahir26-Sep-12 19:06
memberAzim Zahir26-Sep-12 19:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190419.4 | Last Updated 11 Sep 2011
Article Copyright 2011 by Azim Zahir
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid