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
Highlighted
Developer
Posts: 40
Registered: ‎06-04-2014
My Device: Bold 9900

Swap ButtonField image on focus/unfocus

[ Edited ]

I've gone around and around on this. All I'm trying to do is change the image associated with a button when the button gains or loses focus.

 

 

In the calling code, I have the following:

 

...
input = new MyButton(ButtonField.FIELD_HCENTER | ButtonField.CONSUME_CLICK);
        input.setLabel("Enter number");
        input.setMinimalWidth(Display.getWidth()/2);
        input.setFocusImg("ic_rename.png");
        input.setUnfocusImg("ic_rename_black.png");
        input.setImageSize(61, 61);    
        input.setFont(this.getFont().derive(this.getFont().getStyle(), 30));
        input.setLabelRight();

 

 

I have created a class called MyButton which extends the ButtonField class:

 

 

public class MyButton extends ButtonField{
	
	private Image _onFocus, _onUnfocus;
	
	public MyButton(long style) {
		super(style | FOCUSABLE);	
	}
	
	public void setImage(Image image) {
		super.setImage(image);
	}
	
	public void onFocus(int direction) {
		
		this.invalidate();
		this.setImage(_onFocus);
		super.onFocus(direction);
		
	}
	
	private void OnUnfocus() {
		
		this.setImage(_onUnfocus);
		this.invalidate();
		super.onUnfocus();
		
	}
	
	
	public void setUnfocusImg(String filename) {
		_onUnfocus = ImageFactory.createImage(Bitmap.getBitmapResource(filename));
	}
	
	public void setFocusImg(String filename) {
		_onFocus = ImageFactory.createImage(Bitmap.getBitmapResource(filename));
		setImage(_onFocus);
	}


}

 

 

The initial images display just fine, but nothing happens when the buttons lose focus.

 

 

 

 

 

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

Re: Swap ButtonField image on focus/unfocus

You capitalized OnUnfocus. Correct would be onUnfocus.
Add some logging in cases like that to check if is called at all.
You could also use a focuschangelistener.
----------------------------------------------------------
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