Tuesday, June 22, 2010

Creating Buttons in Blackberry

You need to download Blackberry JDE (Java Development Environment) to run the following hello world demo. You can also run the following code on Eclipse by downloading and installing blackberry plug in for Eclipse.
Everything is field in blackberry API. Button is called as ButtonField. Following program will show three buttons. You can add events to these buttons by implementing FieldChangeListener. e.g
-----------------------------------------------------------
ButtonField bf=new ButtonField("Click Me");
    bf.setChangeListener(new Listener1());
-----------------------------------------------------------
public class Listener1 implements FieldChangeListener{
        public void fieldChanged(Field field, int context){
        Dialog.alert("U have clicked Me!");    
        }
    }
-----------------------------------------------------------------------

Following code will display static buttons without event handling.

package src;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.component.ObjectListField;
import net.rim.device.api.ui.Graphics;
import java.util.*;
/**
 *
 */

class ButtonTest extends UiApplication{
    public static void main(String[] args)
    {
        // Create a new instance of the application.
        ButtonTest theApp = new ButtonTest();
        //Constructor of this class will be invoked
      
        // To make the application enter the event thread and start processing messages,
        // we invoke the enterEventDispatcher() method.
        theApp.enterEventDispatcher();
    }
  
    private ButtonTest()
    {
        // Push the main screen instance onto the UI stack for rendering.
  
         pushScreen(new HelloScreen());
         }

}

class HelloScreen extends MainScreen{
  
    HelloScreen(){
  
    ButtonField bf1=new ButtonField("Button one");
    ButtonField bf2=new ButtonField("Button two");
    ButtonField bf3=new ButtonField("Button three");
  
    add(bf1);
    add(bf2);
    add(bf3);
        
    }
  
    public void close(){
    Dialog.alert("Good Bye Folks!!!!!!!!");
    System.exit(0);
    super.close();
    }
}




No comments:

Post a Comment