Help Center

Local Navigation

Create a drop-down list

  1. 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.*;
  2. 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 MyUiScreen class, which is described in step 3, represents the custom screen.
    public class MyUi extends UiApplication 
    { 
        public static void main(String[] args) 
        { 
            MyUi theApp = new MyUi(); 
            theApp.enterEventDispatcher(); 
        } 
        public MyUi() 
        { 
            pushScreen(new MyUiScreen()); 
        } 
    }
  3. 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 MyUiScreen extends MainScreen 
    {
        public MyUiScreen() 
        {
            setTitle("UI Component Sample"); 
        }
    }
  4. In the screen constructor, create a drop-down list that displays a list of words or phrases by using the ObjectChoiceField class. Create a String array to store the items that you want to display in the drop-down list. Create an int to store the default item to display in the drop-down list. In the ObjectChoiceField constructor, specify the label for the drop-down list, the array of items to display, and the default item. In the following code sample, by default Wednesday is displayed. Invoke add() to add the drop-down list to the screen.
    String choices[] = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
    int iSetTo = 2;
    add(new ObjectChoiceField("First Drop-down List",choices,iSetTo));
  5. In the screen constructor, create a second drop-down list that displays a list of numbers by using the NumericChoiceField class. In the NumericChoiceField constructor, specify the label for the drop-down list, the first and last number to display in the drop-down list, the increment to use for the list of numbers, and the default number. In the following code sample, the numeric parameters are stored in int objects. The numbers 1 to 31 are included in the drop-down list and by default the number 10 is displayed. Invoke add() to add the second drop-down list to the screen.
    int iStartAt   = 1;
    int iEndAt     = 31;
    int iIncrement = 1;
    iSetTo         = 10;
    add(new NumericChoiceField("Numeric Drop-Down List",iStartAt,iEndAt,iIncrement,iSetTo));
  6. To override the default functionality that prompts the user to save changes before the application closes, in the extension of the MainScreen class, 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.
    public boolean onSavePrompt()
    {
        return true;
    }

Code sample: Creating a drop-down list

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

public class MyUi extends UiApplication 
{ 
    public static void main(String[] args) 
    { 
        MyUi theApp = new MyUi(); 
        theApp.enterEventDispatcher(); 
    } 
    public MyUi() 
    { 
        pushScreen(new MyUiScreen()); 
    } 
}
class MyUiScreen extends MainScreen 
{
    public MyUiScreen() 
    {
        setTitle("UI Component Sample"); 
        String choices[] = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
        int iSetTo = 2;
        add(new ObjectChoiceField("First Drop-down List",choices,iSetTo));
        int iStartAt   = 1;
        int iEndAt     = 31;
        int iIncrement = 1;
        iSetTo         = 10;
        add(new NumericChoiceField("Numeric Drop-Down List",iStartAt,iEndAt,iIncrement,iSetTo));
    }
    public boolean onSavePrompt()
    {
        return true;
    }
}
Next topic: Labels

Was this information helpful? Send us your comments.