09-24-2009 04:04 PM
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
Solved! Go to Solution.
09-24-2009 04:22 PM
Have a look at the following examples. Might get some clue.
http://www.blackberry.com/knowledgecenterpublic/li
http://supportforums.blackberry.com/rim/board/mess
Regards
Bikas
09-24-2009 04:27 PM
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);}
}
09-24-2009 04:41 PM
Cool. ![]()
If your problem solved, then resolve the thread.
Regards
Bikas
06-03-2011 09:34 AM
hi can i draw an image in an editfield or extends field without override the paint methode ?
06-03-2011 10:46 PM
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