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

Posts: 22
Registered: ‎11-23-2010
My Device: Not Specified
Accepted Solution

EditField Cursor - Vertical Centering



I need a textbox with rounded corners and customized width and height.  Am able to do it by extending Edit Field. But the cursor is always at the top of the field. I want it to be vertically centered.. can someone please suggest me a solution?


Please find the code below for reference



public class TextBoxBorder extends EditField {
 String mDefaultText;
 int BorderColor, FillColor = Color.WHITE, FontColor = Color.BLACK, Width, Height, W = 0, H = 0;
 boolean _inFocus = false;

 public TextBoxBorder(int BorderColor,int FontColor, int Width, int Height,int W,int H,long _st) {
  this.BorderColor = BorderColor;
  this.FontColor = FontColor;
  this.Width = Width;
  this.Height = Height;
  this.W = W;
  this.H = H;

 public void setDefaultText(String DefaultText) {
    	mDefaultText = DefaultText;
 public int getPreferredWidth() {
        return this.Width;
 public int getPreferredHeight() {
        return this.Height;
 public void paint(Graphics g) {
    g.drawRoundRect(0, 0, getPreferredWidth() , getPreferredHeight(),10,10);
    Font font= g.getFont();
    g.drawText(getText(), 2, (getPreferredHeight()-font.getHeight())/2);    
    if ( _inFocus ){
            _inFocus = false;
 public String getDefaultText() {
  	return mDefaultText;
 protected boolean keyChar(char key, int status, int time) {
  if (null != mDefaultText)
   if (getText().equalsIgnoreCase(mDefaultText)) {
    return true;
  return super.keyChar(key, status, time);
 public void layout(int width, int height) {
	width = Math.min( width, getPreferredWidth() );
	height = Math.min(height,getPreferredHeight());
	super.layout(width, height);
	setExtent(width, height);
 protected void onFocus(int direction){
	_inFocus = true;
 protected void onUnfocus(){
		_inFocus = false;
 protected boolean navigationClick(int status, int time) {
	return true;





Posts: 17,019
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: EditField Cursor - Vertical Centering

welcome to the support forums.

i would suggest that you use a manager for your decoration and add the editfield in the center of said manager.

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
Posts: 22
Registered: ‎11-23-2010
My Device: Not Specified

Re: EditField Cursor - Vertical Centering

Thanks Simon... Works like a charm!