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();
    }
}




Sunday, June 20, 2010

Hello World Blackberry Application

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.

package com.rim.samples.device.helloworlddemo;

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.*;

class HelloWorldDemo extends UiApplication
{
     /**
     * Entry point for application.
     */
    public static void main(String[] args)
    {
        // Create a new instance of the application.
        HelloWorldDemo theApp = new HelloWorldDemo();
        // To make the application enter the event thread and start processing messages,
        // we invoke the enterEventDispatcher() method.
        theApp.enterEventDispatcher();
    }

    /**
     *
The default constructor. Creates all of the RIM UI components and pushes the
     * application's root screen onto the UI stack.
    */
    private HelloWorldDemo()
    {
        // Push the main screen instance onto the UI stack for rendering.

        pushScreen(new HelloWorldScreen());
    }  
}


/**
 * Create a new screen that extends MainScreen, which provides default standard
 * behavior for BlackBerry applications.
 */
/*package*/
final class HelloWorldScreen extends MainScreen{

    /**
     * HelloWorldScreen constructor.
     */
    RichTextField rtf;
        
    HelloWorldScreen()
    {
        // Add a field to the title region of the screen. We use a simple LabelField
        // here. The ELLIPSIS option truncates the label text with "..." if the text
        // is too long for the space available.
        LabelField title = new LabelField("Hello World Demo" , LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
        setTitle(title);

        // Add a read only text field (RichTextField) to the screen.  The RichTextField
        // is focusable by default.  In this case we provide a style to make the field
        // non-focusable.
        rtf = new RichTextField("Hello World!" ,Field.NON_FOCUSABLE);
        add(rtf);
    }

    /**
     * Display a dialog box to the user with "Goodbye!" when the application
     * is closed.
     *
     * @see net.rim.device.api.ui.Screen#close()
     */
    public void close()
    {
        // Display a farewell message before closing application.
        Dialog.alert("Goodbye!");
        System.exit(0);
      
        super.close();
    }
}