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
grahamb314
Posts: 95
Registered: ‎03-22-2009
My Device: Torch 9800 & Bold 9700
Accepted Solution

Custom Edit Field - Text not drawing while typing

I have created a custom edit field (code below) but there is no cursor or text drawn while typing.

The text appears if another fields on the screen is clicked, but not while typing. I assume it has something to do with my paint method., but I'm not sure?

Any tips on making the paint methos more efficient would be a bonus!

 

Thanks!

 

import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.FontFamily;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.EditField;

public class CustomTextField2 extends EditField {

	private int fieldWidth;
	private int fieldHeight;
	private int button_colour = 0xF9F9F9;
	private int text_colour = 0x666666;
	private int border_color = 0xCCCCCC;
	private Graphics graphics = null;
	public CustomTextField2(long arg0) {
		super(arg0);
		fieldWidth = Display.getWidth()-100;
		//int off = 8;
		FontFamily fFam = null;
		
		try {
			fFam = FontFamily.forName("SomeFontHere");
		}
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		}  
		
        Font font = fFam.getFont(Font.PLAIN, 20);
        
		Font defaultFont = font;
		int off = 8;
		fieldHeight = defaultFont.getHeight() + off + 10;
		this.setPadding(5, 20, 5, 20);	}
	
	protected void paint(Graphics graphics) {

		graphics.setColor(button_colour);
		graphics.fillRect(0, 0, fieldWidth, fieldHeight);
		graphics.setColor(border_color);
		graphics.drawRect(0, 0, fieldWidth, fieldHeight);
		graphics.setColor(text_colour);
		graphics.drawText(this.getText(),20,10/2);		         super.paint(this.graphics);
	}

	protected void onFocus(int direction) {

		button_colour = 0xF9F9F9;
		text_colour = 0x666666;
		border_color = 0x3598CC;
		this.invalidate();
	}

	protected void onUnfocus() {
		
		button_colour = 0xF9F9F9;
		text_colour = 0x666666;
		border_color = 0xCCCCCC;
		this.invalidate();
	}
	
	protected void drawFocus(Graphics graphics, boolean on) {
		
		  super.drawFocus(graphics, on);
	}

	protected void fieldChangeNotify(int context)
	{
		try {
			
		this.getChangeListener().fieldChanged(this, context);

		}
		catch (Exception e){
			
		}
	}
	
	public int getPreferredHeight() {
		
		return fieldHeight;
	}
	
	public int getPreferredWidth() {
		
		return fieldWidth;
	}
	
	protected void layout(int arg0, int arg1) {
		
		setExtent(getPreferredWidth(), getPreferredHeight());
	}
}

 

Please use plain text.
Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Custom Edit Field - Text not drawing while typing

What is this all about:

 

super.paint(this.graphics);

 

"this.graphics" refers to an object which is set to null.

 

Shouldn't it be

 

super.paint(graphics);

 

??

 

Please use plain text.
Developer
grahamb314
Posts: 95
Registered: ‎03-22-2009
My Device: Torch 9800 & Bold 9700

Re: Custom Edit Field - Text not drawing while typing

[ Edited ]

Sorry, yes, I was removing some pointless code just before I posted this. It actually reads 

super.paint(graphics); 

in my code as you suggest.

 

I've been playing around and found that If I change my layout method to this:

 

public void layout( int width, int height ) {
		
    super.layout( getPreferredWidth(), getPreferredHeight() );
    setExtent(getPreferredWidth(), getPreferredHeight());

}

 From how it was before:

 

protected void layout(int arg0, int arg1) {
		
    setExtent(getPreferredWidth(), getPreferredHeight());

}

 

 

Then I get the cursor back and the output is displayed as it is typed.

However, I get this while typing:

 

example.PNG

 

and after finishing typing, I get this (Perfect!) but only after clicking another field

example 2.PNG

 

[

And unless I comment out this line from the paint method

super.paint(graphics); // Comment this out

  I get this

 

123.PNG

 ]

Please use plain text.
Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Custom Edit Field - Text not drawing while typing

This is "SureType" assistance - it is expected behavior.

 

It comes automatically with devices that have a limited non-qwerty keyboard, or if it is a touch device.

 

 

Please use plain text.
Developer
grahamb314
Posts: 95
Registered: ‎03-22-2009
My Device: Torch 9800 & Bold 9700

Re: Custom Edit Field - Text not drawing while typing

Hi,

Thanks for the reply.

 

By an issue, I mean that the text is there in the field twice. The SureType is great, but I only need text to be written in the center of the field, not at the top.

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

Re: Custom Edit Field - Text not drawing while typing

Just out of curiousity - what are you trying to achieve? What custom features do you need in your EditField?

 

The reason I'm asking is this: EditField is a complicated construct coming with a lot of code baggage - word wrap, cursor painting, very special focus rectangle, copy/paste, etc. Seriously modifying it is asking for trouble. You'd better use super.paint wherever you can - set color and global alpha before this, if you need to, but not much more.

If you need tighter control, extend Field and start from there. You'll have to implement a lot yourself, though.

----------------------------------------------------------
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
grahamb314
Posts: 95
Registered: ‎03-22-2009
My Device: Torch 9800 & Bold 9700

Re: Custom Edit Field - Text not drawing while typing

That doesn't sound too fun!

 

I need the following:

 

Coloured background

Border colour changed when focused (and changed to another colour once unfocused)

The width of the field to be a custom width and the height too, although the height is what seems to be the problem

 

All of the above work now, except that when typing, the text is created at the top of the field and the middle.

 

I suspect that this is because I do this in my paint method? 

graphics.drawText(this.getText(),20,8);//Text Positioning
super.paint(graphics);

or

that my layout method has soemthing wrong:

public void layout( int width, int height ) {
		
super.layout( getPreferredWidth(), getPreferredHeight() );
setExtent(getPreferredWidth(), getPreferredHeight());

}

  - If I comment out the first line from the layout method, I get the text in the middle of the box, as I want, but theere s no cursor and the text doesnt appear unless I click out of the box.

  -If I comment out the last line in the above method instead, I get a screen with half of the field missing, so I need that there for sure.

 

I think it has to be something simple? - Maybe I have a line in the wrong place or I need to call some other method somewhere?  /  Missing an override?

 

The whole issue can be avoided if I simply set the height of the box to the height of the default font. So maybe I need to adjust a variable somewhere to make the cursor appear and type in the middle of thebox, rather than the top?

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

Re: Custom Edit Field - Text not drawing while typing

I've just posted some code in another thread - it does most, if not all, of what you want. Take a look:

Custom stylish EditField (textbox)

----------------------------------------------------------
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
grahamb314
Posts: 95
Registered: ‎03-22-2009
My Device: Torch 9800 & Bold 9700

Re: Custom Edit Field - Text not drawing while typing

I had to change a few bits to get it to work how I want it to, but ultimatly, yes that's what I needed.

 

I still don't understand what was going wrong with my code though, but now i have something functional at last.

 

Thanks for your help on this one.

 

Kudos awarded :smileyhappy:

Please use plain text.