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
New Contributor
Posts: 9
Registered: ‎06-09-2010
My Device: Bold
My Carrier: Rogers
Accepted Solution

custom buttons

Hi,

I have a custom buttonfield that just paints the button and has the text centered and an instance of the button with the following code which should call the loginScreen method when either the trackball is clicked or enter is pressed. However, when I run it enter doesn't seem to work. Please tell me what I am doing wrong. Furthermore, I also want to have the button call loginScreen when the user presses the letter "L". I thought this would also be done using the keyDown method but don't knwo exactly how to do it.

 

public OTButtonField loginButton =  new OTButtonField( LoginApp.getString(
        LoginApp.B_LOGIN))
    {
        protected boolean invokeAction(int action)
        {
            loginScreen();
            return true;
        }
        
        protected boolean keyDown(int keycode,int time)
        {
            switch(keycode)
            {
                case Keypad.KEY_ENTER:
                    loginScreen();
                    return true;
            }
            return false;
        }
    };

 

Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: custom buttons

Why don't you use keyChar() instead of keyDown()?

 

protected boolean keyChar(char c, int status, int time) {
  switch (c) {
    case Characters.ENTER:
    case 'l':
    case 'L':
      loginScreen();
      return true;
    default:
      return super.keyChar(c, status, time);
  }
}

 

Feel free to add other cases as you see fit.  You might also want to check status using KeypadListener constants.

 

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Developer
Posts: 468
Registered: ‎08-07-2009
My Device: Curve, Bold, Storm
My Carrier: Blackberry Developer

Re: custom buttons

hi,

 

this is my custom button class

 

 

/* Customized Button */
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//  Class Name      :       ColorManager
//  Created On      :       22 April 2010
//  Created By      :       Ekansh Srivastava
//  Purpose         :       This class is for Customized Button
////////////////////////////////////////////////////////////////////////////////////////////////////////////

class BigButtonField extends Field
{

    private int backgroundColour; 
    private Bitmap button;
    private int fieldWidth = 60;
    private int fieldHeight = 20;
    private String text;
    private Font fieldFont;
    public BigButtonField(String _text, int color,Font fon) 
    {
        super(Field.FOCUSABLE);
        text = _text;
        if(fon == null)
        {
            fieldFont = FieldFont();
        }
        else
        { 
            fieldFont = fon;
        }
        backgroundColour  = color;
    }
    
    public void setHeight(int height)
    {
        fieldHeight = height;
    }
    
    protected void drawFocus(Graphics graphics, boolean on)
    {
        graphics.setColor(backgroundColour);
        graphics.fillRect(0, 0, fieldWidth, fieldHeight/*,SizeSpec.setX10,SizeSpec.setY10*/); 
        graphics.setColor(Color.WHITE);
        graphics.setFont(fieldFont);
        graphics.drawText(text, ((fieldWidth - fieldFont.getAdvance(text)) / 2),((fieldHeight - fieldFont.getHeight()) / 2));
    }
    
    protected void onFocus(int direction) {
        invalidate();
    }
    
    protected void onUnfocus() {
        invalidate();
    }
    
    public int getPreferredWidth() {
        return fieldWidth;
    }
    
    public int getPreferredHeight() {
        return fieldHeight;
    }
    
    protected void layout(int arg0, int arg1) {
        setExtent(getPreferredWidth(), getPreferredHeight());
    }
    
    public Font FieldFont() 
    {
        try {
            FontFamily theFam = FontFamily.forName("SYSTEM");
            return theFam.getFont(net.rim.device.api.ui.Font.BOLD, 15);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        return null;
    }
    
    protected void paint(Graphics graphics) 
    {
        graphics.drawRect(0, 0, fieldWidth, fieldHeight/*,SizeSpec.setX10,SizeSpec.setY10*/); 
        graphics.setFont(fieldFont);
        graphics.drawText(text, ((fieldWidth - fieldFont.getAdvance(text)) / 2),((fieldHeight - fieldFont.getHeight()) / 2)); 
    }
    
    protected void fieldChangeNotify(int context) 
    {
        try {
                this.getChangeListener().fieldChanged(this, context);
                
            } 
            catch (Exception exception) 
            {
            }
    }
    
    
}

 

 

And use this class to make button

 

 

btnBrowsePlayer = new BigButtonField("LIST",Color.BLACK,Font.getDefault().derive( Font.PLAIN, 15))
{
protected boolean navigationClick(int status, int time)
{
loginScreen();
return true; } }; btnBrowsePlayer.setFont(Font.getDefault().derive( Font.PLAIN, 15));

 For keys to work....Enter and L

    public boolean keyChar(char key, int status, int time) 
    {
        boolean retval = false;
        switch (key) 
        {
            case Characters.ESCAPE:
                retval = true;
                break;

            case 'L':
                 break;
    case Keypad.KEY_ENTER:

           default:
                break;
        }       
        return retval;
    }
    
     public boolean keyDown(int keycode, int time) 
    {
        return false;
    }

    /* Implementation of KeyListener.keyRepeat(). */
    public boolean keyRepeat(int keycode, int time) 
    {
        return false;
    }
    
    /* Implementation of KeyListener.keyStatus(). */
    public boolean keyStatus(int keycode, int time) 
    {
        return false;
    }
    
    /* Implementation of KeyListener.keyUp(). */
    public boolean keyUp(int keycode, int time) 
    {
        return false;
    }

 

 

 

 

 

You need implement KeyListener

 

 

    public boolean keyChar(char key, int status, int time) 
    {
        boolean retval = false;
        switch (key) 
        {
            case Keypad.KEY_ENTER://dstroying objects created
                retval = true;
                break;
            case 'L'://ur code
           break;

           default:
                break;
        }       
        return retval;
    }
    
     public boolean keyDown(int keycode, int time) 
    {
        return false;
    }

    /* Implementation of KeyListener.keyRepeat(). */
    public boolean keyRepeat(int keycode, int time) 
    {
        return false;
    }
    
    /* Implementation of KeyListener.keyStatus(). */
    public boolean keyStatus(int keycode, int time) 
    {
        return false;
    }
    
    /* Implementation of KeyListener.keyUp(). */
    public boolean keyUp(int keycode, int time) 
    {
        return false;
    }

 

 

 

 

 

----------------------------------------------------------
feel free to press the kudos button on the left side to thank the user that helped you.
please mark posts as solved if you found a solution.