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
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: EditField / Centering user input

I think the issue here is that paint is called by drawFocus, which only has a context that includes the areas that the Field thinks need to be repainted on focus.  So clear will not clear the whole Field, probably only the left hand side that the EditField thinks is where its text is.

 

I would try overriding onFocus and onUnfocus and do an invalidate in them before calling 'super....'.  Let us know if that helps. 

Developer
Developer
TEP
Posts: 42
Registered: ‎02-17-2009
My Device: Not Specified

Re: EditField / Centering user input

From what I was seeing when I would touch the field or give focus to the field but not enter the field to change it, the text would shift to the left and repaint itself leaving some characters on the screen. This I believe lines up with what you are saying about paint only needing to paint the area consumed by the text rather than the whole field.

 

In any event, I have added onFocus/onUnfocus to the field and it appears to have fully resolved all of the issues I was having. Here is the final code I ended up with:

 

 

efName = new EditField("", "", 30, EditField.FIELD_HCENTER | EditField.NO_NEWLINE) {
	public void paint(Graphics g){
		if (isFocus()) {
			invalidate();
			super.paint(g);
		} else {
			g.drawText(efName.toString(), 0, 0, DrawStyle.HCENTER, getContentWidth());
		}
	}
	protected void onFocus(int direction) {
		super.onFocus(direction);
		invalidate();
	}
	protected void onUnfocus(){
		super.onUnfocus();
		invalidate();
	}
};

 

I would like to thank both of you again for your help. There is quite a bit to learn and even some of the things that seem simple enough can become complicated. If it weren't for this Java Development forum, I think I would really be stuck.

 

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: EditField / Centering user input

Sorry you still have something here that I and I suspect arkadyz do not like.

 

Please, please, please remove the invalidate() in the paint. 

 

You should not need it.  And it has potential to cause you grief. 

 

Let us know if you remove it and things stop working. 

Developer
Developer
TEP
Posts: 42
Registered: ‎02-17-2009
My Device: Not Specified

Re: EditField / Centering user input

Peter/Arkadyz,

 

I have removed the invalidate(). I am not sure how I managed to put it back in. The code is still working to center the text when focus is lost and left align the text when focus is gained allowing to a user to edit the field normally. Here is what I am using as final code for this editfield.

 

 

efName = new EditField("", "", 30, EditField.FIELD_HCENTER | EditField.NO_NEWLINE) {
	public void paint(Graphics g){
		if (isFocus()) {
			super.paint(g);
		} else {
			g.drawText(efName.toString(), 0, 0, DrawStyle.HCENTER, getContentWidth());
		}
	}
	protected void onFocus(int direction) {
		super.onFocus(direction);
		invalidate();
	}
	protected void onUnfocus(){
		super.onUnfocus();
		invalidate();
	}
};

 

Thank you for your help. I learned quite a bit even though it was something that isn't overly complicated.

 

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: EditField / Centering user input

Excellent - thanks for letting us know.