Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
eicheled
Posts: 77
Registered: ‎04-01-2011
My Device: Bold 9700
Accepted Solution

Wrong FieldChangeListener invoked for custom button

 

I have two custom buttons, creatively call them ButtonA and ButtonB (they extend Field).

 

The buttons are on a custom HorizontalFieldManager (call it CHFM).

 

Each button is assigned it's OWN FieldChangeListener.

 

When I press ButtonA, the listener for A is called and all is well.

 

However when I press ButtonB, the listener for A is called ! (Instead of B's listener).

 

Looking at the stack, the listener for B is invoked because the application 'thinks' B has been pressed.

 

ButtonB(Field).fieldChangeNotify(int) line: 1667   

ButtonB.navigationClick(int, int) line: 39   

CHFM(Manager).navigationClick(int, int) line: 3960   

CHFM.navigationClick(int, int) line: 39   


I've been trying to change the behaviour by altering the navigationClick() method in CHFM, but to no avail.

 

The listeners are created and assigned to the buttons when the buttons are created, before they are added to CHFM.

 

Any suggestions would be appreciated.

 

Thanks in advance.

 

 

Please use plain text.
Super Contributor
pp
Posts: 278
Registered: ‎11-04-2010
My Device: 4.5 and above

Re: Wrong FieldChangeListener invoked for custom button

Hi,

 For custombuttons check this

 

http://supportforums.blackberry.com/t5/Java-Development/Best-way-to-create-transparent-buttons/m-p/1...

 

For any thing touch or click this should work.

This should be in class extending mainscreen implmenting FieldChangeListener

        HorizontalFieldManager newButtonManager = new HorizontalFieldManager();
        button_delete = new customButtonField("Delete Line",(Display.getWidth())/2);
        button_delete.setAlignment(customButtonField.ALIGNMENT_CENTER);
        button_addEngagement = new customButtonField("Add Engagement",(Display.getWidth())/2);
        button_addEngagement.setAlignment(customButtonField.ALIGNMENT_CENTER);
        button_delete.setChangeListener(this);
        button_addEngagement.setChangeListener(this);
        newButtonManager.add(button_delete); newButtonManager.add(button_addEngagement);

 

now implement FieldChanged method in that class

 

public void fieldChanged(Field field, int context){
        if(field instanceof customButtonField){
            if(field == button_copy){
            }else if(field == button_save){
                Dialog.ask(Dialog.D_OK,"Timesheet saved as Draft, but not submitted!",Dialog.OK);
            }else if(field == button_submit){
                Dialog.ask(Dialog.D_OK,"Timesheet submitted",Dialog.OK);
            }else if(field == button_delete){
            }else if(field == button_addEngagement){
            }
        }

}

 

 

This should work

 

Thanks & Regards

             pp

 

Please use plain text.
Developer
simon_hain
Posts: 16,190
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Wrong FieldChangeListener invoked for custom button

did you create your buttons with the CONSUME_CLICK style? If not, they will execute the default action of a navigationclick, show the context menu.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
eicheled
Posts: 77
Registered: ‎04-01-2011
My Device: Bold 9700

Re: Wrong FieldChangeListener invoked for custom button

@pp - thanks for the link, however this doesn't seem to be a simple button creation problem.

 

@simon_hain Yes, I use CONSUME_CLICK

 

ButtonA functions perfectly.

 

ButtonB behaves (properly) like ButtonA. ButtonB also acts like it is ButtonA. I don't understand why the click event from ButtonB is being consumed by ButtonA's listener.

 

 

Please use plain text.
Trusted Contributor
QuestionMan
Posts: 167
Registered: ‎06-30-2011
My Device: n/a

Re: Wrong FieldChangeListener invoked for custom button

I saw that you said you made each button have its own field listener. Try this, it might fix it.

 

public void fieldChanged(Field field, int context)
    {

        if(field==button1)

             {

               blablabla

       }

       else if (field==button2)

      {

          blablabla

     }

}

 

works for all my buttons, hope it helps.

Please use plain text.
Developer
eicheled
Posts: 77
Registered: ‎04-01-2011
My Device: Bold 9700

Re: Wrong FieldChangeListener invoked for custom button

I've done some more experimenting, and have some more information. I completely disabled the listener on ButtonB, and the behaviour persisted (as I expected it might).

 

However, I've now discovered that if I click ANYWHERE on my custom HorizontalFieldManager, the listener for ButtonA is called.

 

I don't ever explicitly do anything with listeners on the HFM, at least not intentionally. My custom HFM uses USE_ALL_WIDTH, and simply tries to place my two buttons on either side of the manager.

 

 

 

Please use plain text.
Developer
eicheled
Posts: 77
Registered: ‎04-01-2011
My Device: Bold 9700

Re: Wrong FieldChangeListener invoked for custom button

@QuestionMan - thanks, I tried that already. The fieldChanged only ever gets called from ButtonA, never from ButtonB, so the 'else' is never executed.

Please use plain text.
Super Contributor
pp
Posts: 278
Registered: ‎11-04-2010
My Device: 4.5 and above

Re: Wrong FieldChangeListener invoked for custom button

Hi,

ok change the navigationClick itself like this and dont setChangeListener for buttons

 

protected boolean navigationClick(int status, int time) { 

          if(UiApplication.getUiApplication().getActiveScreen().getLeafFieldWithFocus() instanceof CustomButton){
            final CustomButtonfocus = (CustomButton)UiApplication.getUiApplication().getActiveScreen().getLeafFieldWithFocus();
            if (  (focus != null)  &&  (focus instanceof CustomButton)  ) {

                   if(focus == buttonA){

 

                    }else if(focus == buttonB){

 

                    }

             }

          }
        return true;    
    } 

 

OR one more type is

 

FieldChangeListener clear_listener = new FieldChangeListener() {
                public void fieldChanged(Field field, int context) {
                    }
                }
            };
CustomButtonField buttonA = new CustomButtonField ("Clear", CONSUME_CLICK|FIELD_VCENTER);
 buttonA.setChangeListener(clear_listener);

//ADD TO CHFM

 

create same to buttonB also

 

 

Thanks & Regards

              pp

 

 

Please use plain text.
Developer
peter_strange
Posts: 19,607
Registered: ‎07-14-2008
My Device: Not Specified

Re: Wrong FieldChangeListener invoked for custom button

I presume you are doing this on a TouchScreen.

 

You need to override touchEvent and make sure that the click was within your Field's extents.  Here is some RIM supplied code that does this;

 

    protected boolean touchEvent(TouchEvent message)
    {
        boolean isOutOfBounds = touchEventOutOfBounds( message );
        switch(message.getEvent()) {
            case TouchEvent.DOWN:
                if( !isOutOfBounds ) {
                    if( !_active ) {
                        _active = true;
                        invalidate();
                    }
                    return true;
                }
                return false;
            case TouchEvent.UNCLICK:
                if( _active ) {
                    _active = false;
                    invalidate();
                }
               
                if( !isOutOfBounds ) {
                    clickButton();
                    return true;
                }
            case TouchEvent.UP:
                if( _active ) {
                    _active = false;
                    invalidate();
                }
           
            default :
                return false;
        }
    }
   
    private boolean touchEventOutOfBounds( TouchEvent message )
    {
        int x = message.getX( 1 );
        int y = message.getY( 1 );
        return ( x < 0 || y < 0 || x > getWidth() || y > getHeight() );
    }

Please use plain text.
Developer
eicheled
Posts: 77
Registered: ‎04-01-2011
My Device: Bold 9700

Re: Wrong FieldChangeListener invoked for custom button

First, thanks for the suggestions. I'm however still confused.

 

The buttons extend Field because of another issue I had - as described in this thread here

 

So now if I change the buttons to extend ButtonField (instead of Field), my click/listener problem goes away, however the problem that I had in the first place (as described on the other thread) comes back.

 

 

Please use plain text.