02-04-2011 08:28 AM
Hello,
I am trying to make a LabelField act like a ButtonField in that it will be clickable to perform an event. So I have created my own field that extends LabelField. I have also set the new field to change text color based upon whether the field is active or not. The field is changing colors on focus without a problem. The one problem that I am having is this:
Whenever I register the field for a change listener (field.setChangeListener) and then I go to run the code, the field is automatically being clicked repeatedly in a loop of what seems like every second. This happens without me clicking on the field. When I remove the setChangeListener(), this doesn't happen. I tried using a ButtonField and everything works fine, so it must be a problem with my custom field. Would someone mind taking a look at my code? Thanks!
public class LinkField extends LabelField {
private boolean active;
public LinkField(String text, long style) {
super(text, style);
Font appFont = Font.getDefault().derive(Font.BOLD, 7, Ui.UNITS_pt);
setFont(appFont);
}
protected void drawFocus(Graphics graphics, boolean on) {
}
protected void paint(Graphics graphics) {
if (active) {
graphics.setColor(0x0054a6);
} else {
graphics.setColor(Color.RED);
}
super.paint(graphics);
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(1);
return true;
}
protected void onFocus(int direction) {
active = true;
invalidate();
super.onFocus(direction);
}
protected void onUnfocus() {
active = false;
invalidate();
super.onUnfocus();
}
public boolean keyChar(char key, int status, int time) {
if (key == Characters.ENTER) {
fieldChangeNotify(0);
return true;
}
return false;
}
}
Solved! Go to Solution.
02-04-2011 09:15 AM
Do you mean it's firing your fieldChangeNotify every second, or looks like it's gaining focus?
02-04-2011 09:28 AM
02-04-2011 09:30 AM - edited 02-04-2011 09:31 AM
You may try overriding fieldChangeNotify() to only be allowed to call super.fieldChangeNotify() when the correct set of circumstances are met. For instance set a flag before you call it in your navigationClick method and then check for that in fieldChangeNotify. If it exists, call super.fieldChangeNotify and reset the flag. If it doesn't, just ignore it.
02-04-2011 09:55 AM
Have you set the LabelField to be focusable?
You could try
public LinkField(String text, long style) {
super(text, style | LinkField.FOCUSABLE);
Font appFont = Font.getDefault().derive(Font.BOLD, 7, Ui.UNITS_pt);
setFont(appFont);
}
02-04-2011 10:15 AM
Remove calling super.onFocus and super.onUnfocus from your overrides - they never achieve anything and might easily mess up your field's inner works.
Even a more robust solution - make your field implement FocusChangeListener, call setFocusListener(this) in the constructor and move all your active = ... + invalidate() logic into the focusChanged method. See if the problem persists.
And yes, heed the advice of BeMor and make your Field FOCUSABLE (alternatively, override isFocusable and control its return via other methods).
02-04-2011 02:01 PM
Hi jprofitt,
I have added the following, which seems to work:
protected void fieldChangeNotify(int context){
if(context == 1){
try {
this.getChangeListener.fieldChanged(this, context);
} catch (Exception e){
}
}
}
So navigationClick() will set a flag of 1, and that will be recognized in the above fieldChangeNotify(). I just want to make sure that I did this properly...
Thanks!
02-04-2011 02:02 PM
I removed the super.onFocus and super.onUnfocus calls, thanks. They didn't cause the problem, but if they don't really do anything, then I guess there is no need in having them..
02-04-2011 02:04 PM - edited 02-04-2011 02:04 PM
Yep that's what I was talking about, but dont' forget to reset your flag. Hope it works out for you!
02-04-2011 02:07 PM
Thanks for your help!