If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

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 custom LabelField

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;
	}

}

 

 

Please use plain text.
Developer
jprofitt
Posts: 604
Registered: ‎12-27-2010
My Device: Not Specified

Re: Trouble with custom LabelField

Do you mean it's firing your fieldChangeNotify every second, or looks like it's gaining focus?

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

Re: Trouble with custom LabelField

I believe it is firing the fieldChangeNotify every second. I tried commenting out the fieldChaneNotify(1) and the problem still happens though. I also tried commenting out the onFocus and onUnFocus and it still happens. The only way I can gwt it to stop is by removing the setChangeListener on the field...
Please use plain text.
Developer
jprofitt
Posts: 604
Registered: ‎12-27-2010
My Device: Not Specified

Re: Trouble with custom LabelField

[ Edited ]

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.

Please use plain text.
Developer
BeMor
Posts: 171
Registered: ‎08-20-2008
My Device: Not Specified

Re: Trouble with custom LabelField

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);
	}

 

 

Please use plain text.
Developer
arkadyz
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Trouble with custom LabelField

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).

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Please use plain text.
Developer
behrk2
Posts: 367
Registered: ‎11-25-2009
My Device: Not Specified

Re: Trouble with custom LabelField

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!

 

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

Re: Trouble with custom LabelField

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..

Please use plain text.
Developer
jprofitt
Posts: 604
Registered: ‎12-27-2010
My Device: Not Specified

Re: Trouble with custom LabelField

[ Edited ]

Yep that's what I was talking about, but dont' forget to reset your flag. Hope it works out for you!

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

Re: Trouble with custom LabelField

Thanks for your help!

Please use plain text.