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
New Developer
wizzz
Posts: 12
Registered: ‎09-22-2009
My Device: Not Specified
Accepted Solution

Autosizing EditField

I worked on making an EditField autosize according to the MaxSize value and paint rectangle around:

 

the following code works fine inline:

 

EditField EF = new EditField("",null,8,EditField.FILTER_REAL_NUMERIC)

{

public void layout(int width, int height)

{

int x = getFont().getAdvance("0") * getMaxSize();

 super.layout(x,getPreferredHeight());

setExtent(x,getPreferredHeight());

} protected void paint(Graphics g)

{

g.setColor(Color.
GRAY);

g.drawRect(0,0,getWidth(),getHeight());

g.setColor(Color.BLACK);

super.paint(g);

}

};

 

 

 

but when i build a class to extend EditField, it doesn't work well (the width is not set right)

 

public class SizeableEditField extends EditField

{

 

public SizeableEditField()

{

new EditField();

}

public SizeableEditField(long style)

{

new EditField(style);

}

public SizeableEditField(String label, String initialValue)

{

new EditField(label, initialValue);

}

public SizeableEditField(String label, String initialValue,int maxNumChars, long style)

{

new EditField(label, initialValue, maxNumChars, style);

}

 

public void layout(int width, int height)

{

int x = this.getFont().getAdvance("0") * this.getMaxSize(); /*get the size of the field in pixels from maxChar*/

super.layout(x,this.getPreferredHeight()); /*layout with that max space in mind */

setExtent(x,this.getPreferredHeight()); /*set extend of the field for manager*/

}

protected void paint(Graphics g)

{

g.setColor(Color.
GRAY);

g.drawRect(0,0,this.getWidth(),this.getHeight());g.setColor(Color.

BLACK); super.paint(g);

}

}

 

 calling with:

SizeableEditField EF = new SizeableEditField("",null,8,EditField.FILTER_REAL_NUMERIC);

The rectangle is painted but the size is still the fullwidth, so it seems like x is not calculated right.

Any idea why? My debugger doesn't let me see Expression results, os it is hard to figure out.

 

Thanks for help.

 

Regards

 

Please use plain text.
Developer
bikas
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: Autosizing EditField

Please use plain text.
New Developer
wizzz
Posts: 12
Registered: ‎09-22-2009
My Device: Not Specified

Re: Autosizing EditField

I actually just found the problem:

I need to call super instead of a new EditField

this works:

 

 

public class SizeableEditField extends EditField

{

 

public SizeableEditField()

{

super();

}

public SizeableEditField(long style)

{

super(style);

}

public SizeableEditField(String label, String initialValue)

{

super(label, initialValue);

}

public SizeableEditField(String label, String initialValue,int maxNumChars, long style)

{

super(label, initialValue, maxNumChars, style);

}

 

public void layout(int width, int height)

{

int x = getFont().getAdvance("0") * getMaxSize(); /*get the size of the field in pixels from maxChar*/

super.layout(x,getPreferredHeight()); /*layout with that max space in mind */

this.setExtent(x,getPreferredHeight()); /*set extend of the field for manager*/

}

protected void paint(Graphics g)

{

g.setColor(Color.
GRAY);

g.drawRect(0,0,this.getWidth(),this.getHeight());g.setColor(Color.

BLACK); super.paint(g);

}

}

Please use plain text.
Developer
bikas
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: Autosizing EditField

Cool. :smileyhappy:

 

If your problem solved, then resolve the thread.

 

Regards

Bikas

 

Please use plain text.
Developer
ahmadahmad
Posts: 404
Registered: ‎05-31-2011
My Device: curve
My Carrier: devel

Re: Autosizing EditField

hi can i draw an image in an editfield or extends field without override the paint methode ?

Please use plain text.
Developer
welsenKiriono
Posts: 202
Registered: ‎04-19-2011
My Device: Blackberry Bold 9780
My Carrier: TAM

Re: Autosizing EditField

i think you should post a new thread, instead using a solved thread.. ^^

As far as i know, Field can drawing image only in method paint...

 

correct me if i'm wrong

Please use plain text.