views:

4026

answers:

4

I have created a ButtonField & a BitmapField like..

  public class MyCanvas extends MainScreen implements FieldChangeListener
  {
    HorizontalFieldManager hfm;
    private Bitmap startBitmap;
    private BitmapField startBitmapField;
    private ButtonField okButton;

   MyCanvas()
   {
     hfm = new HorizontalFIeldManager();
     startBitmap = Bitmap.getBitmapResource("start.png"); 
     startBitmapField = new BitmapField(startBitmap);
     startBitmapField.setChangeListener(this); 
     hfm.add(startBitmapField);

     okButton = new ButtonField("Ok", ButtonField.CONSUME_CLICK | ButtonField.NEVER_DIRTY); 
     okButton.setChangeListener(this);
     hfm.add(okButton);
   }

   public void fieldChanged(Field field, int context)
   {
    if(field == startBitmapField)
    {
        System.out.println("Touched START...");
    }
    else if(field == okButton)
    {
        System.out.println("Touched Ok...");
    }
   }
}

But the ButtonField or BitmapField click is not happening in Blackberry 4.7 simulator.

I want to build it for Blackberry Storm so I m using Blackberry 4.7

How to handle click/touch events for ButtonField & BitmapField for Blackberry Storm?

+2  A: 

Hi!
First of all, don't forget to add hfm to screen ;)
Actually button click works fine.
Now, to make bitmap click works as well, implement protected boolean touchEvent(TouchEvent message) for your BitmapField. It will be better to create extended class:

class MyCanvas extends MainScreen implements FieldChangeListener {
    HorizontalFieldManager hfm;
    private Bitmap startBitmap;
    private BitmapField startBitmapField;
    private ButtonField okButton;
    private ButtonField cancelButton;

    MyCanvas() {
     hfm = new HorizontalFieldManager();
     add(hfm);

     startBitmap = Bitmap.getBitmapResource("start.png");
     startBitmapField = new TouchBitmapField(startBitmap);
     startBitmapField.setChangeListener(this);
     hfm.add(startBitmapField);

     okButton = new ButtonField("Ok", ButtonField.CONSUME_CLICK
       | ButtonField.NEVER_DIRTY);
     okButton.setChangeListener(this);
     hfm.add(okButton);

     cancelButton = new ButtonField("Cancel", ButtonField.CONSUME_CLICK
       | ButtonField.NEVER_DIRTY);
     cancelButton.setChangeListener(this);
     hfm.add(cancelButton);
    }

    public void fieldChanged(Field field, int context) {
     if (field == startBitmapField) {
      System.out.println("Touched START...");
     } else if (field == okButton) {
      System.out.println("Touched Ok...");
     } else if (field == cancelButton) {
      System.out.println("Touched Cancel...");
     }
    }
}

class TouchBitmapField extends BitmapField {
    public TouchBitmapField(Bitmap startBitmap) {
     super(startBitmap);
    }

    protected boolean touchEvent(TouchEvent message) {
     if (TouchEvent.CLICK == message.getEvent()) {
      FieldChangeListener listener = getChangeListener();
      if (null != listener)
       listener.fieldChanged(this, 1);
     }
     return super.touchEvent(message);
    }
}
Max Gontar
Shreyas
Shreyas, I've updated code with one more button, it's still working... Maybe it depends on something else? Check it, and if it's still not working, you may post your full code so we will figure it out.
Max Gontar
Shreyas
sorry about that. I've forgot to mention, I'm using Storm 9530 default simulator from eclipse plugin components 4.7.0.46 and it's working fine.
Max Gontar
Shreyas
I think this issue and maybe many more (as I can see, 41 is _beta_ version) may be solved simply - download newer version. You can take Eclipse Software Update for the BlackBerry JDE v4.7 Component Pack from http://na.blackberry.com/eng/developers/javaappdev/javaeclipseplug.jsp or newer simulator from http://na.blackberry.com/eng/developers/resources/simulators.jsp
Max Gontar
I used this to infer how regular (non touch screen) button pressing is done.
Justin Dearing
A: 

I m creating the ButtonField & BitmapFields as

okButtonField = new ButtonField("Ok", BitmapField.HIGHLIGHT_SELECT | BitmapField.FOCUSABLE);

startBitmapField = new BitmapField(startBitmap, BitmapField.HIGHLIGHT_SELECT | BitmapField.FOCUSABLE);

Its working with..

protected boolean touchEvent(TouchEvent event)
{
  switch( event.getEvent()  ) 
  {
    case TouchEvent.DOWN:  ........
   return true;
    case TouchEvent.MOVE: .......
                            return true;
    case TouchEvent.UP: ........ 
                            return true;   

    case TouchEvent.CLICK:
      if(deleteButton.isFocus())
      {            
        System.out.println("Touched DEL ..........");
      }
      else if(okButton.isFocus())
      {            
        System.out.println("Touched OK ..........");
      }   
  else if(startBitmapField.isFocus())
      {            
        System.out.println("Touched START ..........");
      }         
    return true;
  }
 return false;
 }

but everytime the same button is invoked which is having focus.

Means if "Ok" button is having focus then even though u clicked on "Delete" button "Ok" button is called.

So how to change the focus on Button Click? means whichever ButtonField or BitmapField is clicked, should get the focus?

is there any method to check "button.isClicked() like button.isFocus() " ?

Shreyas
Do u know how to CONSUME_CLICK for BitmapFields like for BittonField okButton = new ButtonField("Ok", ButtonField.CONSUME_CLICK ?
Shreyas
this touch event worked for me
SWATI
A: 

Do anyone know how to CONSUME_CLICK for BitmapField like for ButtonField ?

ButtonField okButton = new ButtonField("Ok", ButtonField.CONSUME_CLICK);

I am creating BitmapField like..

BitmapField start = new BitmapField(startBitmap, BitmapFieid.FOCUSABLE);
Shreyas
A: 

same problem. Is your problem solved.

Jasbeer