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
behrk2
Posts: 367
Registered: ‎11-25-2009
My Device: Not Specified
Accepted Solution

Trouble with JDE Sample PictureBackgroundButtonField.java

[ Edited ]

Hi everyone,

 

I am using the JDE sample PictureBackgroundButtonField.java to create a button with an image on it. It works fine, except when I load my application, the button is "in focus" (shows the _onPicture), and will not go out of focus. Can anyone figure this out? Thanks!

 

Here is the button instantiation:

 

 

PictureBackgroundButtonField pic = new PictureBackgroundButtonField("Hello", DrawStyle.HCENTER);
		hfmBottom.add(pic);

 

 

Here is the PictureBackgroundButtonField.java:

 

 

/**
 * PictureBackgroundButtonField.java
 *
 * Copyright © 1998-2009 Research In Motion Ltd.
 * 
 * Note: For the sake of simplicity, this sample application may not leverage
 * resource bundles and resource strings.  However, it is STRONGLY recommended
 * that application developers make use of the localization features available
 * within the BlackBerry development platform to ensure a seamless application
 * experience across a variety of languages and geographies.  For more information
 * on localizing your application, please refer to the BlackBerry Java Development
 * Environment Development Guide associated with this release.
 */

package com.kflicks.ui.Custom;

import net.rim.device.api.ui.*;
import net.rim.device.api.system.*;

/**
 * Custom button field that shows how to use images as button backgrounds.
 */
public class PictureBackgroundButtonField extends Field 
{       
    private String _label;
    private int _labelHeight;
    private int _labelWidth;
    private Font _font;
    
    private Bitmap _currentPicture;
    private Bitmap _onPicture = Bitmap.getBitmapResource("res/search_on.png");
    private Bitmap _offPicture = Bitmap.getBitmapResource("res/search_off.png");
        
    /**
     * Constructor.
     * @param text - the text to be displayed on the button
     * @param style - combination of field style bits to specify display
           attributes 
     */
    public PictureBackgroundButtonField(String text, long style) 
    {
        super(style);
        
        _font = getFont();
        _label = text;
        _labelHeight = _font.getHeight();
        _labelWidth = _font.getAdvance(_label);
        _currentPicture = _onPicture;
    }
    
    /**
     * @return The text on the button
     */
    String getText()
    {
        return _label;
    }
    
    /**
     * Field implementation.
     * @see net.rim.device.api.ui.Field#getPreferredHeight()
     */
    public int getPreferredHeight() 
    {
        return _labelHeight + 4;
    }

    /**
     * Field implementation.
     * @see net.rim.device.api.ui.Field#getPreferredWidth()
     */
    public int getPreferredWidth() 
    {
        return _labelWidth + 8;
    }
    
    /**
     * Field implementation.  Changes the picture when focus is gained.
     * @see net.rim.device.api.ui.Field#onFocus(int)
     */
    protected void onFocus(int direction) 
    {
        _currentPicture = _onPicture;
        invalidate();
    }

    /**
     * Field implementation.  Changes picture back when focus is lost.
     * @see net.rim.device.api.ui.Field#onUnfocus()
     */
    protected void onUnfocus() 
    {
        _currentPicture = _offPicture;
        invalidate();
    }
    
    /**
     * Field implementation.  
     * @see net.rim.device.api.ui.Field#drawFocus(Graphics, boolean)
     */
    protected void drawFocus(Graphics graphics, boolean on) 
    {
        // Do nothing
    }
    
    /**
     * Field implementation.
     * @see net.rim.device.api.ui.Field#layout(int, int)
     */
    protected void layout(int width, int height) 
    {
        setExtent(Math.min( width, getPreferredWidth()), 
        Math.min( height, getPreferredHeight()));
    }

    /**
     * Field implementation.
     * @see net.rim.device.api.ui.Field#paint(Graphics)
     */
    protected void paint(Graphics graphics) 
    {       
        // First draw the background colour and picture
        graphics.setColor(0x00EE3526);
        graphics.fillRect(0, 0, getWidth(), getHeight());
        graphics.drawBitmap(0, 0, getWidth(), getHeight(), _currentPicture, 0, 0);
        
        // Then draw the text
        /*graphics.setColor(Color.BLACK);
        graphics.setFont(_font);
        graphics.drawText(_label, 4, 2, 
            (int)( getStyle() & DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK ),
            getWidth() - 6 );*/
    }
        
    /**
     * Overridden so that the Event Dispatch thread can catch this event
     * instead of having it be caught here..
     * @see net.rim.device.api.ui.Field#navigationClick(int, int)
     */
    protected boolean navigationClick(int status, int time) 
    {
        fieldChangeNotify(1);
        return true;
    }
    
}

 

 

Please use plain text.
Developer
hades_6
Posts: 178
Registered: ‎08-26-2009
My Device: Not Specified

Re: Trouble with JDE Sample PictureBackgroundButtonField.java

Looks fine,

 

What do you mean by will not go out of focus? When you create the button, it will be the image onFocus. If you want to give it focus you have to add the focusable parameter,

 

So your code should look like,

 

 

 

PictureBackgroundButtonField pic = new PictureBackgroundButtonField("Hello", Field.FOCUSABLE | DrawStyle.HCENTER);

 

 

Please use plain text.
Developer
behrk2
Posts: 367
Registered: ‎11-25-2009
My Device: Not Specified

Re: Trouble with JDE Sample PictureBackgroundButtonField.java

Adding the Field.FOCUSABLE did the trick, thanks!

Please use plain text.