BlackBerry Forums Support Community

BlackBerry Forums Support Community (http://www.blackberryforums.com/)
-   Developer Forum (http://www.blackberryforums.com/developer-forum/)
-   -   How to determine which button is clicked? (http://www.blackberryforums.com/developer-forum/12560-how-determine-button-clicked.html)

timinator 07-25-2005 07:55 PM

How to determine which button is clicked?
 
I'm a rookie at the BB stuff but an experienced developer. In looking at the samples and doing searches via Google, I've yet to come across an example of how to determine which of two buttons have been clicked with the trackwheel. I currently have the following code, modified from the Hello World sample:

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

class SalutationScreen extends MainScreen
{
public SalutationScreen()
{
super();
LabelField applicationTitle = new LabelField("Hello World Title");
ButtonField testButton = new ButtonField("Click Me", FOCUSABLE);
ButtonField orButton = new ButtonField("Or Me", FOCUSABLE);
setTitle(applicationTitle);
add(testButton);
add(orButton);
}
public boolean onClose()
{
Dialog.alert("Bye World!");
System.exit(0);
return true;
}
// Invoked when the trackwheel is clicked.
public boolean trackwheelClick( int status, int time )
{
Dialog.alert("Button clicked. Status:" + status) ;
return true;
}
}


Along with the associated HelloWorld.java routine. How do I determine which button was clicked?

Thanks in advance for the help.

Tim

jhfisher 07-26-2005 04:41 AM

use a field change listener.

timinator 07-26-2005 12:02 PM

Duh! How about an example? Or at least point me to a sample that demonstrates how this works with multiple buttons.

timinator 07-26-2005 01:33 PM

Well, I finally figured this out. I'm surprised that something that should be so easy to do was so difficult to figure out. I guess it's due to the fact that I'm a C & C# developer, just now getting into java. Plus, the fact that the BB does things a little differently. Anyway, for the benefit of anyone else trying to figure this out, here's my sample code for determing which button is being selected. Enjoy.

Code:

// HelloWorld.java

class HelloWorld extends net.rim.device.api.ui.UiApplication
{
    public static void main(String[] args)
    {
        HelloWorld instance = new HelloWorld();
        instance.enterEventDispatcher();
    } 
    public HelloWorld()
    {
        pushScreen(new SalutationScreen());
    }
}

And here's the 2nd piece:
Code:

//SalutationScreen.java

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

public class SalutationScreen extends MainScreen implements FieldChangeListener
{
    ButtonField testButton;
    ButtonField orButton;
    LabelField applicationTitle;
   
    public SalutationScreen()
    {
        super();
        applicationTitle = new LabelField("Hello World Title");
        setTitle(applicationTitle);
       
        testButton = new ButtonField("Click Me");
        testButton.setChangeListener(this);
        add(testButton);
       
        orButton = new ButtonField("Or Me");
        orButton.setChangeListener(this);
        add(orButton);
    }
   
    public void fieldChanged(Field field, int context)
    {
        if (field == testButton)
        {
            Dialog.inform("Click Me Button clicked.");
        }
        if (field == orButton)
        {
            Dialog.inform("Or Me Button clicked.");
        }
    }

    public boolean onClose()
    {
        Dialog.alert("Bye World!");   
        System.exit(0);
          return true;
    }
}



All times are GMT -5. The time now is 08:33 PM.

Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.