5

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?


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() " ?

Nate
  • 31,017
  • 13
  • 83
  • 207
Shreyas
  • 209
  • 1
  • 7
  • 16

1 Answers1

5

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);
    }
}
Maksym Gontar
  • 22,765
  • 10
  • 78
  • 114
  • yes. I added hfm to screen. Actually there r 2 buttons. So how to handle its click events. Now i m handing by button.setChangeListener(this) & overriding its fieldChanged() method as my code above. But no effect. So how to handle touch events for 2 buttons? I will try ur code for bitmapFields. – Shreyas Aug 31 '09 at 04:55
  • 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. – Maksym Gontar Aug 31 '09 at 10:39
  • can u touch ButtonFields & BitmapFields on Simulator 4.7.0.41? – Shreyas Aug 31 '09 at 11:15
  • 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. – Maksym Gontar Aug 31 '09 at 13:33
  • I m working in Blackberry JDE 4.7.0.41 & tested in Blackberry 9500 & 9530 Simulators but its not working. ButtonFields & BitmapFields Touch Events r not invoked. Not getting what may be the problem. Plz suggest. – Shreyas Sep 01 '09 at 11:01
  • 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 – Maksym Gontar Sep 01 '09 at 11:56
  • I used this to infer how regular (non touch screen) button pressing is done. – Justin Dearing Nov 29 '09 at 15:50
  • I generally think UIs are better if they trigger actions off the UNCLICK (a.k.a. "touch up") event, not when the user presses down (TouchEvent.CLICK). This gives the user the opportunity to say "ooops! I didn't mean to do that", and abort the action. – Nate Aug 28 '12 at 09:44