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

Re: Wrong FieldChangeListener invoked for custom button

I've been dealing with a few issues at the same time, and they seem to be at least partially related. I fixed the problem in the other thread I mentioned, so now I'm trying to focus on this.

 

Now that I've changed the buttons to extend ButtonField, the problem has changed.

 

ButtonA still works properly. The problem with clicking anywhere on my custom HorizontalFieldManager is also gone.

 

The problem is now that clicking on ButtonB does nothing.

 

I tested by putting identical buttons into the manager, and one works and the other doesn't.

 

Can I assume that this is a problem with my custom HorizontalFieldManager, and not the buttons themselves?

 

This is my manager:

 

 

import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.HorizontalFieldManager;

class BFHM extends HorizontalFieldManager {

    private ButtonField leftButton;
    private ButtonField rightButton;
    private final static int TOP_MARGIN = 5;
    private final static int LEFT_MARGIN = 5;

    public BFHM() {
        super( USE_ALL_WIDTH );
    }

	public void sublayout( int maxWidth, int maxHeight ) {

        super.sublayout( maxWidth, maxHeight );
        int width = getWidth();

        if (rightButton != null ) {
            int x = width - rightButton.getWidth() - LEFT_MARGIN;
            int y = TOP_MARGIN;
            setPositionChild( rightButton, x, y );
        }
        if (leftButton != null) {
        	int y = TOP_MARGIN;
        	int x = LEFT_MARGIN;
            setPositionChild( leftButton, x, y );
        }
        setExtent( maxWidth, leftButton.getHeight() + TOP_MARGIN * 2 );
    }
	
    public void setLeftButton( ButtonField leftButton ) {
        this.leftButton = leftButton;
        super.add(leftButton);
    }

    public void setRightButton( ButtonField rightButton ) {
        this.rightButton = rightButton;
        super.add(rightButton);
    }

}

 

It simply places 2 buttons on either side of the field.

 

Any suggestions appreciated.

 

 

Super Contributor
pp
Posts: 278
Registered: ‎11-04-2010
My Device: 4.5 and above

Re: Wrong FieldChangeListener invoked for custom button

Hi,

     This worked for me.

/*
 * BFHM.java
 *
 * © <your company here>, 2003-2008
 * Confidential and proprietary.
 */


import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.Field;
import net.rim.device.api.system.Display;

public class BFHM extends Manager {

    private ButtonField leftButton;
    private ButtonField rightButton;
    private final static int TOP_MARGIN = 5;
    private final static int LEFT_MARGIN = 5;

    public BFHM() {
        super( USE_ALL_WIDTH );
    }
    protected void sublayout(int width, int height) {
        //set the left field
        Field leftField = getField(0);
        if (leftField != null && equals(leftField.getManager())) {  
            int y = TOP_MARGIN;
            int x = LEFT_MARGIN;
            layoutChild(leftField,width/3,height);
            setPositionChild(leftField, x, y);
        }
        
        // start with the right Field 
        Field rightField = getField(1);
        if (rightField != null && equals(rightField.getManager())) { 
            int x = width - rightField.getWidth() - LEFT_MARGIN;
            int y = TOP_MARGIN;
            layoutChild(rightField, width/3,height);
            setPositionChild( rightField, x, y );
        }
    
        //To set the required dimensions for the field
        setExtent(width,height);
    }

}

 just add fields to this .

 

Thanks & Regards

              pp

 

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

Re: Wrong FieldChangeListener invoked for custom button

Excellent, thank you! I had something similar, but couldn't get layoutChild to seem to work properly, and I see now that it is needed.

 

One more quick question (more about style) if I may. I modified the setExtent() call to use a height just barely tall enough to give room for the buttons and the margins that I want. The example code you supplied caused the manager to take up the entire screen. Should I be supplying a value just large enough for what I need, or is there a better way to do it?

 

 

Super Contributor
pp
Posts: 278
Registered: ‎11-04-2010
My Device: 4.5 and above

Re: Wrong FieldChangeListener invoked for custom button

Hi,
you can override getPreferredWidth() and getPreferredHeight() and provide those in sublayout like this
setExtent(getPreferredWidth(), getPreferredHeight());

check this link

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800505/800508/...
Developer
eicheled
Posts: 77
Registered: ‎04-01-2011
My Device: Bold 9700

Re: Wrong FieldChangeListener invoked for custom button

I knew that :-), I almost ashamed I asked!

 

Thanks for your help.

New Developer
CobiRic
Posts: 21
Registered: ‎03-31-2011
My Device: Developer

Re: Wrong FieldChangeListener invoked for custom button

fwiw I came across this thread, also having problems with the wrong button responding to the touch.

 

In my case, the advice here and in other threads did not help. For me, the problem was that my buttons were in a custom manager I had made.

 

In my custom manager in the sublayout() method, I was calling super.sublayout(), and then laying out some special fields myself. The solution in my case was to NOT call super.sublayout() from my custom manager. That was the only change required - the buttons (button-styled custom fields) I used did not need updating.

 

I mention this in case it helps someone in the future with similar problem, since this seems to be a common question.

www.cobiinteractive.com
www.madmob.co.za