5 Replies Latest reply: Apr 3, 2017 10:48 PM by omar123456789 RSS

custom button not listening to setChangeListener

najirm Newbie

I'm using Blackberry JDK ver 5.0.

 

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

 

Implement advanced buttons, fields, and managers

 

But the button is not accepting any click?

 

Any help?

  • Re: custom button not listening to setChangeListener
    crispyoz Novice

    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");
        }
    }

  • Re: custom button not listening to setChangeListener
    najirm Newbie

    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);

  • Re: custom button not listening to setChangeListener
    crispyoz Novice

    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

  • Re: custom button not listening to setChangeListener
    omar123456789 Novice

    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;
        }