Search code examples
blackberrybuttonfieldchangelistener

blackberry ButtonField application sample


I am very new in black berry application.

now trying to create a calculator in blackberry using eclipse:

so i have added a button (ButtonField), my first target is when press this button i want display

"hi.. now you can try with text field."

here i put my code, please go through it.

Launcher.java

public class Launcher extends UiApplication {
    public static void main(String[] args) {
        Launcher theApp = new Launcher();
        theApp.enterEventDispatcher();
   }
   private Launcher()
   {
       this.pushScreen(new MainScrn());
   }

}

MainScrn .java

public class MainScrn extends MainScreen implements FieldChangeListener {
    public MainScrn() {
        LabelField lf_hello = new LabelField();
        lf_hello.setText("Hello, World!");
        lf_hello.setBackground(BackgroundFactory.createSolidBackground(124));
        ButtonField mySubmitButton = new ButtonField("clickMe");
        mySubmitButton.setChangeListener(this);
        this.add(lf_hello);
        this.add(mySubmitButton);
    }

    public void fieldChanged(Field field, int context) {
        System.out.println("hi.. now you can try with text field");

    }
}

hello whats the wrong on this. ? pls help.. it will quite simple for you, but me not now ?


Solution

  • Check this out.

    public final class MyScreen extends MainScreen implements FieldChangeListener
    {
    /**
     * Creates a new MyScreen object
     */
    
    LabelField lbl = new LabelField("hi.. now you can try with text field.");
    ButtonField bf = new ButtonField("Click Me",ButtonField.CONSUME_CLICK);
    public MyScreen()
    {        
        // Set the displayed title of the screen       
        setTitle("MyTitle");
        bf.setChangeListener(this);
        add(bf);
    
    }
    
    public void fieldChanged(Field field, int context) {
        // TODO Auto-generated method stub
        if(field == bf)
        {
            add(lbl);
        }
    }
    }