BlackBerry Forums Support Community

Closed Thread
LinkBack Thread Tools
Old 08-01-2005, 08:06 PM   #1 (permalink)
Knows Where the Search Button Is
timinator's Avatar
Join Date: Jul 2005
Location: Washington
Model: 8300
Carrier: AT&T
Posts: 29
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Example of Edit Fields and Buttons

Please Login to Remove!

I've been asking my share of "rookie" Blackberry questions here, and some of what I've found has been helpful and some has not. There seems to be a big gap between the "Hello World" and the UI samples in the docs and on the forums. So, I figured I would post a small sample that demonstrates how to display Input fields and buttons, and how to show the contents of the Input fields when specific buttons are clicked. Feel free to comment on and make improvements. Here's the code:

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

// Class --------------------------------------------------------------------------
public class MyTextInput2 extends net.rim.device.api.ui.UiApplication implements FieldChangeListener
    private AutoTextEditField inputField;                   //define the edit fields
    private AutoTextEditField anotherField;
    private LabelField appTitle;                            //application title label
    private ButtonField showInputButton;                    //define the buttons
    private ButtonField anotherInputButton;
    public static void main(String[] args)
        MyTextInput2 TIinstance = new MyTextInput2();       //create an instance of this class app
        TIinstance.enterEventDispatcher();                  //define a handle for the event processor
    public MyTextInput2()
        MainScreen inputScreen = new MainScreen();          //create screen instance
        appTitle = new LabelField("Input Test2");           //define app title
        inputField = new AutoTextEditField("Input1: ","");  //define edit fields
        anotherField = new AutoTextEditField("Input2: ","");
        showInputButton = new ButtonField("Show Input1 value");     //define button titles
        anotherInputButton = new ButtonField("Show Input2 value");
        showInputButton.setChangeListener(this);            //define the button handles for the event processor
        inputScreen.setTitle(appTitle);                     //post the screen title
        inputScreen.add(inputField);                        //post the input fields on screen
        inputScreen.add(showInputButton);                   //post the buttons on the screen
        pushScreen(inputScreen);                            //post the screen itself

    public void fieldChanged(Field field, int context) //respond to button events
        if (field == showInputButton) //if first button selected
            Dialog.alert(inputField.getText()); //show text from first input field
        if (field == anotherInputButton) //if second button selected
            Dialog.alert(anotherField.getText()); //show text from second input field
    public boolean onClose() //fires if app is closed
            return true;
"There are 10 types of people in the world.
Those that understand binary and those that don't!" - The ART of S/W Development
Old 08-03-2005, 11:43 AM   #2 (permalink)
Knows Where the Search Button Is
Join Date: Feb 2005
Location: New Mexico
Posts: 26
Post Thanks: 0
Thanked 0 Times in 0 Posts

Thanks. I noticed an option in the menu to save, but this wasn't implemented. How to save this data?
Old 08-03-2005, 11:53 AM   #3 (permalink)
BlackBerry Extraordinaire
Join Date: Dec 2004
Location: in a house...
Model: lots
Carrier: Rogers
Posts: 1,148
Post Thanks: 0
Thanked 0 Times in 0 Posts

Good example, you might want to override the trackwheelListener. As well, you don't need to enter the entire class path for the class.

public class MyTextInput2 extends UiApplication implements FieldChangeListener
would be sufficient.

What I like to do is override the FieldChangeListener on each button individually like this...

ButtonField testButton = new ButtonField("Click Me");
    testButton.setChangeListener(new FieldChangeListener() 
        public void fieldChanged(Field field, int context) 
             Dialog.alert("Button Clicked!");
It helps me keep the code for the event close to the button object.

Old 08-14-2005, 05:52 PM   #4 (permalink)
Knows Where the Search Button Is
Join Date: Feb 2005
Posts: 18
Post Thanks: 0
Thanked 0 Times in 0 Posts

I am not sure I like either one of these approaches. The first one does not allow me to encapsulate the screen object. The application could get very ugly if it had several screens with all of the objects declared global to the application class.

The second approach (I am not sure about this) appears that the fieldChanged method call would not have access to any of the other objects that are on the screen. Is this the case? Could the fieldChanged method see other fields that are implemented on the main screen to get to their data?

The good thing is it is possible to implement the fieldChangedListner on a subclass of MainScreen. This would allow multiple screens to exist within an application. The screen would then be encapsulated.

Guess its good there is so much flexibility here regarding how to implement this.
Closed Thread

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Fein 913036K01 Vacuum Bags for 9-55-13 & 9-55-13PE 3-Pack
Fein 913036K01 Vacuum Bags for 9-55-13 & 9-55-13PE 3-Pack pictureTranscat Dual Pressure Vacuum Calibrator 5860PV Hand Pump -24" Hg to 500 PSI
Transcat Dual Pressure Vacuum Calibrator 5860PV Hand Pump -24Thermocouple Vaccum Gage Tube Cat. No. 8651860G5
Thermocouple Vaccum Gage Tube Cat. No. 8651860G5 pictureQIAvac Manifold 6s Vacuum Manifold
QIAvac Manifold 6s Vacuum Manifold pictureDoyle Web Vacuum System 80" Wide Web
Doyle Web Vacuum System 80

Copyright 2004-2016
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.