Help Center

Local Navigation

Create a check box

  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 check box by using the CheckboxField class. In the CheckboxField constructor, specify the label for the check box and use a Boolean value to indicate whether the check box is the default selection (for example,true indicates that by default the check box is selected). Invoke add() to add the check box to the screen.
    add(new CheckboxField("First Check Box", true));
    add(new CheckboxField("Second Check Box", false));
  5. To change the default behavior of a check box, use the styles that are inherited from the Field class. For example, to create a check box that users cannot change, use the READONLY style.
    add(new CheckboxField("First Check Box", true, this.READONLY));
  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 check box

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"); 
        add(new CheckboxField("First Check Box", true));
        add(new CheckboxField("Second Check Box", false));
    }
    public boolean onSavePrompt()
    {
        return true;
    }
}
Next topic: Create a bitmap

Was this information helpful? Send us your comments.