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
staticx99
Posts: 24
Registered: ‎07-22-2009
My Device: Not Specified
Accepted Solution

setChangeListener doesn't catch on custom buttons

I've made some custom buttons (extends field) and overrided  the navigation click in it :

 

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

 

I put these buttons in a GridFieldManager but the setChangeListener event is never catched when I click the button... any idea?

 

Thanks! 

 

Please use plain text.
Developer
Posts: 5,339
Registered: ‎09-20-2008
My Device: ***
My Carrier: ***

Re: setChangeListener doesn't catch on custom buttons

Try this:


protected boolean navigationClick(int status, int time) {
fieldChangeNotify(1);
return super.navigationClick(status, time);
}

 

 

 

Please use plain text.
Developer
staticx99
Posts: 24
Registered: ‎07-22-2009
My Device: Not Specified

Re: setChangeListener doesn't catch on custom buttons

[ Edited ]

Thanks for the answer but still nothing here.

 

If I replace my custom button that extends field by a normal button , everything works fine so I suppose the problem is in my custom button class.

 

Here's my code :

 

custombutton class ->

 

 

package...

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

public class customButton extends Field {

private Bitmap _currentPicture;
private Bitmap _onPicture;
private Bitmap _offPicture;
private int width;
private int height;

customButton (String onImage, String offImage,int w, int h, long style){
super(style);
_offPicture = Bitmap.getBitmapResource(offImage);
_onPicture = Bitmap.getBitmapResource(onImage);

_currentPicture = _onPicture;
width=w;
height=h;

}

public int getPreferredHeight(){
return width;
}

public int getPreferredWidth(){
return height;
}

protected void onFocus(int direction){
_currentPicture = _offPicture;
invalidate();
}

protected void onUnfocus(){
_currentPicture = _onPicture;
invalidate();
}

protected void layout(int width, int height) {
setExtent(Math.min( width, getPreferredWidth()), Math.min( height, getPreferredHeight()));
}

protected void paint(Graphics graphics) {
//graphics.setBackgroundColor(Color.BLACK);
graphics.clear();
graphics.fillRect(0, 0, getWidth(), getHeight());
graphics.drawBitmap(0, 0, getWidth(), getHeight(), _currentPicture, 0, 0);
}

protected boolean navigationClick(int status, int time) {
fieldChangeNotify(1);
return super.navigationClick(status, time);
}

}

 

 

 


 

MainScreen -->

 

 

GridFieldManager gridFieldManager = new GridFieldManager(new int[] {Graphics.getScreenWidth()/2, Graphics.getScreenWidth()/2}, 130, 0); // Row 1 customButton btn1 = new customButton("img/btn.jpg","img/btnDown.jpg",120,120,Field.FOCUSABLE | Field.FIELD_VCENTER | Field.FIELD_RIGHT); btn1.setChangeListener(this.btn1_click); gridFieldManager.add(btn1); ... subManager.add(gridFieldManager); mainManager.add(subManager); this.add(mainManager); ... public FieldChangeListener btn1_click = new FieldChangeListener() { public void fieldChanged(Field field, int context) { ... } };

 


 

 

 

 

 Thanks!

 

Message Edited by staticx99 on 08-03-2009 10:11 AM
Please use plain text.
Developer
Posts: 5,339
Registered: ‎09-20-2008
My Device: ***
My Carrier: ***

Re: setChangeListener doesn't catch on custom buttons

Code you've posted is not readable.
Please use plain text.
Developer
simon_hain
Posts: 16,209
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: setChangeListener doesn't catch on custom buttons

it is correct to return true, no need for the super call, as the click should be consumed.
the call to fieldChangeNotify works for me, with the slight modification that i have overwritten it empty in the field itself (to avoid bitmap changed events) and am calling super.fieldChangeNotify
----------------------------------------------------------
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
Please use plain text.
Developer
staticx99
Posts: 24
Registered: ‎07-22-2009
My Device: Not Specified

Re: setChangeListener doesn't catch on custom buttons

[ Edited ]

Yep i'm sorry , tried to edit it 4 times but the "insert code" button send it like that even if its perfectly readable when i preview it.

I'll try to fix that 

 

There it goes... the "insert code" doesn't works well with Chrome :smileyhappy:

Message Edited by staticx99 on 08-03-2009 10:11 AM
Please use plain text.
Developer
simon_hain
Posts: 16,209
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: setChangeListener doesn't catch on custom buttons

you'll have to create the changelistener before you set it. And don't mix local variables with class variables.
----------------------------------------------------------
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
Please use plain text.
Developer
staticx99
Posts: 24
Registered: ‎07-22-2009
My Device: Not Specified

Re: setChangeListener doesn't catch on custom buttons

Not quite sure about what you said...

 

Here my main code : 

 

 

package com.rim.... import net.rim.blackberry.api.browser.Browser; import net.rim.blackberry.api.browser.BrowserSession; import net.rim.device.api.ui.UiApplication; import net.rim.device.api.ui.container.*; import net.rim.device.api.ui.component.*; import net.rim.device.api.ui.*; import net.rim.device.api.ui.Field; import net.rim.device.api.system.*; import net.rim.device.api.ui.decor.*; import net.rim.device.api.ui.Graphics; class ticketAcces extends UiApplication { public ticketAccesScreen _ticketScreen; public static void main(String[] args) { ticketAcces theApp = new ticketAcces(); theApp.enterEventDispatcher(); } private ticketAcces() { pushScreen(new ticketAccesScreen()); } } final class ticketAccesScreen extends MainScreen { private VerticalFieldManager mainManager; private VerticalFieldManager subManager; private int deviceWidth = Display.getWidth(); private int deviceHeight = Display.getHeight(); private HorizontalFieldManager field; RichTextField _content; connection _connect; String resultat; String[] ArrayResultats=new String[10]; public ticketAccesScreen() { super(NO_VERTICAL_SCROLL); //this manager is used for the static background image mainManager = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL | Manager.NO_VERTICAL_SCROLLBAR ) { public void paint(Graphics graphics) { graphics.clear(); //graphics.drawBitmap(0, 0, deviceWidth, deviceHeight, _backgroundBitmap, 0, 0); //graphics.drawBitmap(0, 0, _header.getWidth(), _header.getHeight(), _header, 0, 0); super.paint(graphics); } }; Background bg = BackgroundFactory.createSolidBackground(0x00cec1b6); mainManager.setBackground(bg); subManager = new VerticalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR ) { protected void sublayout( int maxWidth, int maxHeight ) { int displayWidth = deviceWidth; int displayHeight = deviceHeight; super.sublayout( displayWidth, displayHeight); setExtent( displayWidth, displayHeight); } }; subManager.setBackground(bg); field = new HorizontalFieldManager(NO_HORIZONTAL_SCROLL | NO_VERTICAL_SCROLL | USE_ALL_WIDTH); Bitmap img = Bitmap.getBitmapResource("img/headerBig.png"); BitmapField header = new BitmapField(img); header.setSpace(Graphics.getScreenWidth()/2 - img.getWidth()/2,5); field.add(header); subManager.add(field); GridFieldManager gridFieldManager = new GridFieldManager(new int[] {Graphics.getScreenWidth()/2, Graphics.getScreenWidth()/2}, 130, 0); // Row 1 btn1 = new customButton("img/btn.jpg","img/btnDown.jpg",120,120,Field.FOCUSABLE | Field.FIELD_VCENTER | Field.FIELD_RIGHT); customButton btn2 = new customButton("img/btn.jpg","img/btnDown.jpg",120,120,Field.FOCUSABLE | Field.FIELD_VCENTER | Field.FIELD_LEFT); btn1.setChangeListener(btn1_click); btn2.setChangeListener(btn1_click); gridFieldManager.add(btn1); gridFieldManager.add(btn2); // Row 2 customButton btn3 = new customButton("img/btn.jpg","img/btnDown.jpg",120,120,Field.FOCUSABLE | Field.FIELD_VCENTER | Field.FIELD_RIGHT); customButton btn4 = new customButton("img/btn.jpg","img/btnDown.jpg",120,120,Field.FOCUSABLE | Field.FIELD_VCENTER | Field.FIELD_LEFT); btn3.setChangeListener(btn1_click); btn4.setChangeListener(btn1_click); gridFieldManager.add(btn3); gridFieldManager.add(btn4); subManager.add(gridFieldManager); mainManager.add(subManager); this.add(mainManager); } FieldChangeListener btn1_click = new FieldChangeListener() { public void fieldChanged(Field field, int context) { ... } }; public void close() { Dialog.alert("Goodbye!"); System.exit(0); super.close(); } }

 Thanks!

 

 

Please use plain text.
Developer
staticx99
Posts: 24
Registered: ‎07-22-2009
My Device: Not Specified

Re: setChangeListener doesn't catch on custom buttons

Hummm well ... it works now but only with the trackball press ... is there a simple way to make it works with the "enter" button?
Please use plain text.
Developer
simon_hain
Posts: 16,209
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: setChangeListener doesn't catch on custom buttons

the enter-button is not a navigationclick. you can use keydown, keychar or a similar method to handle it.
----------------------------------------------------------
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
Please use plain text.