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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
New Contributor
Posts: 4
Registered: ‎09-05-2011
My Device: BlackBerry 9800
My Carrier: Vodafone

custom button not listening to setChangeListener

I'm using Blackberry JDK ver 5.0.

 

I've implmented a Custom Button Field(Embossed Buttonfield) from here:

 

http://supportforums.blackberry.com/t5/Java-Development/Implement-advanced-buttons-fields-and-manage...

 

But the button is not accepting any click?

 

Any help?

Developer
Posts: 228
Registered: ‎10-01-2011
My Device: Passport

Re: custom button not listening to setChangeListener

Najirm it is better to paste some of your code beause you may have missed something, I suspected there is no listener but if you post code we can see.

 

Chris

New Contributor
Posts: 4
Registered: ‎09-05-2011
My Device: BlackBerry 9800
My Carrier: Vodafone

Re: custom button not listening to setChangeListener

BaseButtonField.java

 

public abstract class BaseButtonField extends Field
{
    private XYRect _drawFocusTempRect = new XYRect();
    
    public BaseButtonField()
    {
        this( 0 );
    }
    
    public BaseButtonField( long style )
    {        
        super( Field.FOCUSABLE | style );
    }
        
    protected void layout( int width, int height )
    {
        setExtent( 10, 10 );
    }
    
    protected void drawFocus( Graphics g, boolean on )
    {
        getFocusRect( _drawFocusTempRect );
    
        boolean oldDrawStyleFocus = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS );
        boolean notEmpty = g.pushContext( _drawFocusTempRect.x, _drawFocusTempRect.y, _drawFocusTempRect.width, _drawFocusTempRect.height, 0, 0 );

        try {
            if( notEmpty ) {
                g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, on );
                paintBackground( g );
                paint( g );
            }
        } finally {
            g.popContext();
            g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, oldDrawStyleFocus );
        }
    }
    
    protected boolean keyChar( char character, int status, int time )
    {
        if( character == Characters.ENTER ) {
            clickButton();
            return true;
        }
        return super.keyChar( character, status, time );
    }
    
    protected boolean navigationClick( int status, int time )
    {
        clickButton();
        return true;    
    }
    
    protected boolean trackwheelClick( int status, int time )
    {        
        clickButton();    
        return true;
    }
    
    protected boolean invokeAction( int action )
    {
        switch( action ) {
            case ACTION_INVOKE: {
                clickButton();
                return true;
            }
        }
        return super.invokeAction( action );
    }    

    public void setDirty( boolean dirty ) {
        // We never want to be dirty or muddy
    }
     
    public void setMuddy( boolean muddy ) {
        // We never want to be dirty or muddy
    }
         
    /**
     * A public way to click this button
     */
    public void clickButton()
    {
        fieldChangeNotify( 0 );
    }
}

 

 

EmbossedButtonFiel.java

 

 

public class EmbossedButtonField extends BaseButtonField
{
    public static final long COLOUR_BORDER              = 0xc5fd60b0047307a1L; //net.rim.device.api.ui.component.EmbossedButtonField.COLOUR_BORDER
    public static final long COLOUR_TEXT                = 0x16a6e940230dba6bL; //net.rim.device.api.ui.component.EmbossedButtonField.COLOUR_TEXT
    public static final long COLOUR_TEXT_FOCUS          = 0xe208bcf8cb684c98L; //net.rim.device.api.ui.component.EmbossedButtonField.COLOUR_TEXT_FOCUS
    public static final long COLOUR_BACKGROUND          = 0x8d733213d6ac8b3bL; //net.rim.device.api.ui.component.EmbossedButtonField.COLOUR_BACKGROUND
    public static final long COLOUR_BACKGROUND_FOCUS    = 0x3e2cc79e4fd151d3L; //net.rim.device.api.ui.component.EmbossedButtonField.COLOUR_BACKGROUND_FOCUS

    private static final int XPADDING = Display.getWidth() > 320 ? 7 : 5;   // TODO: Touchscreen should be different
    private static final int YPADDING = Display.getWidth() > 320 ? 4 : 3;   // TODO: Touchscreen should be different
    private static final int BEVEL    = 2;
    
    private LongIntHashtable _colourTable;
    private Font _buttonFont;
    private String _text;
    private boolean _pressed;
    
    private int _width;
    private int _height;
    
    public EmbossedButtonField( String text )
    {
        this(text, 0);
    }
    
    public EmbossedButtonField( String text, long style )
    {
        this( text, style, null );
    }
    
    public EmbossedButtonField( String text, long style, LongIntHashtable colourTable )
    {
        super( Field.FOCUSABLE | style);
        _text = text;
        setColourTable( colourTable );
    }

    public void setColourTable( LongIntHashtable colourTable )
    {
        _colourTable = colourTable;
        invalidate();
    }
    
    public int getColour( long colourKey )
    {
        if( _colourTable != null ) {
            int colourValue = _colourTable.get( colourKey );
            if( colourValue >= 0 ) {
                return colourValue;
            }
        }
            
        // Otherwise, just use some reasonable default colours
        if( colourKey == COLOUR_BORDER ) {
            return 0x212121;
        } else if( colourKey == COLOUR_TEXT ) {
            return 0xD6D6D6;
        } else if( colourKey == COLOUR_TEXT_FOCUS ) {
            return Color.WHITE;
        } else if( colourKey == COLOUR_BACKGROUND ) {
            return isStyle( Field.READONLY ) ? 0x777777 : 0x424242;
        } else if( colourKey == COLOUR_BACKGROUND_FOCUS ) {
            return isStyle( Field.READONLY ) ? 0x666688 : 0x185AB5;
        } else {
            throw new IllegalArgumentException();
        }
    }
    
    public void setText(String text)
    {
        _text = text;
    }
    
    public void applyFont()
    {
        _buttonFont = getFont().derive(Font.BOLD);
    }
    
    public int getPreferredWidth()
    {
        return 2 * XPADDING + _buttonFont.getAdvance( _text );
    }

    protected void onUnfocus()
    {
        super.onUnfocus();
        if( _pressed ) {
            _pressed = false;
            invalidate();
        }
    }
    
    protected boolean navigationClick(int status, int time) {
        _pressed = true;
        invalidate();
        return super.navigationClick( status, time );
    }
    
    protected boolean navigationUnclick(int status, int time) {
        _pressed = false;
        invalidate();
        return true;
    }
    
    public int getPreferredHeight()
    {
        return 2 * YPADDING + _buttonFont.getHeight();
    }
        
    protected void layout( int width, int height )
    {
        setExtent( isStyle( USE_ALL_WIDTH ) ? width : getPreferredWidth(), getPreferredHeight() );
        _width = getWidth();
        _height = getHeight();
    }
    
    protected void paint( Graphics g )
    {
        int oldColour = g.getColor();
        Font oldFont = g.getFont();
        try {
            g.setFont(_buttonFont);
            g.setColor( g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? getColour( COLOUR_TEXT_FOCUS ) : getColour( COLOUR_TEXT ) );
            g.drawText( _text, 0, YPADDING, DrawStyle.HCENTER, _width );
        } finally {
            g.setFont( oldFont );
            g.setColor( oldColour );
        }
    }
    
    protected void paintBackground( Graphics g)
    {
        int oldColour = g.getBackgroundColor();
        int oldAlpha = g.getGlobalAlpha();
        try {
            // Border
            g.setColor( getColour( COLOUR_BORDER ) );
            g.fillRect( 1, 0, _width - 2, _height );
            g.fillRect( 0, 1, _width,     _height - 2 );
            
            // Base color
            g.setColor( g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? getColour( COLOUR_BACKGROUND_FOCUS ) : getColour( COLOUR_BACKGROUND ) );
            g.fillRect( 1, 1, _width - 2, _height - 2 );
            
            // Highlight and lowlight
            g.setGlobalAlpha( 0x44 );
            g.setColor( _pressed ? Color.BLACK : Color.WHITE );
            g.fillRect( 1, 1, _width - 2, BEVEL );
            g.setColor( _pressed ? Color.WHITE : Color.BLACK );
            g.fillRect( 0, _height - BEVEL - 1, _width, BEVEL );
            
            // Base color
            g.setGlobalAlpha( 0xFF );
            g.setColor( g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? getColour( COLOUR_BACKGROUND_FOCUS ) : getColour( COLOUR_BACKGROUND ) );
            g.fillRect( 2, 2, _width - 4, _height - 4 );
            
        } finally {
            g.setBackgroundColor( oldColour );
            g.setGlobalAlpha( oldAlpha );
        }
    }
}

 

 

and implemented as:

 

 

EmbossedButtonField button = new EmbossedButtonField ("Click", ButtonField.CONSUME_CLICK);
button .setChangeListener(this);

Developer
Posts: 228
Registered: ‎10-01-2011
My Device: Passport

Re: custom button not listening to setChangeListener

Your button works fine on OS5, here is the test screen code, which will pop up a dialog.  Are you sure yo are not using your mouse to click the button a non-touch screen simulator?

 

public final class MyScreen extends MainScreen implements FieldChangeListener
{

 

    public MyScreen()
    {
         // Set the displayed title of the screen
         setTitle("MyTitle");

         EmbossedButtonField button = new EmbossedButtonField ("Click", ButtonField.CONSUME_CLICK);
         button.setChangeListener(this);
         add(button);
}

 

   public void fieldChanged(Field field, int context) {
         Dialog.inform("Button pressed");
    }
}

Developer
Posts: 17,026
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: custom button not listening to setChangeListener

try http://www.blackberry.com/developers/docs/7.1.0api/net/rim/device/api/ui/Field.html#getChangeListene... check for null, if not null call fieldChanged.
----------------------------------------------------------
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
Trusted Contributor
Posts: 103
Registered: ‎11-07-2010
My Device: Not Specified

Re: custom button not listening to setChangeListener

Hi najirm,

 

paste the following two methods in your custom button and try again,

 

    public boolean isFocusable() {
    
            return true;
    }

 

protected boolean navigationClick(int status, int time) {
        fieldChangeNotify(0);
        return true;
    }