09-23-2010 10:55 AM
Hi,
I'm interested to know how could I set the height of a EditField and have the text written at the middle of the editfield .
Thanks !
09-23-2010 11:00 AM
depends on what you really want to achieve.
you can use a custom layout manager to place your editfield into the middle of some empty space.
you can also set the height of an editfield by overwriting getPreferredHeight.
you cannot control where the text is written, this would require a custom field.
09-23-2010 11:02 AM
hello,
here is code.
here is the code for you
class MyEditField extends EditField
{
public MyEditField (long style)
{
super (style);
setBorder (BorderFactory.createSimpleBorder(new XYEdges (1, 1, 1, 1)));
}
public int getPreferredHeight()
{
return Font.getDefault.getHeight() * 3; //setting the height of edit field as 3 rows
}
public void layout (int width, int height)
{
super.layout (width, height);
if (getExtent().height < getPreferredHeight())
setExtent (width, getPreferredHeight());
}
}
thanks,
ajay
accept solution if it works.
09-24-2010 01:53 AM
Indeed the height of the EditField is customizable in this way , but when I write in it the text goes at the top of the EditField and not at the middle as I would like
09-24-2010 02:53 AM
hi,
class EditFieldClass extends EditField
{
int width,height;
EditFieldClass(long style,int width,int height)
{
super(style);
this.width = width;
this.height = height;
}
protected void layout(int width, int height)
{
width = this.width;
height = this.height;
super.layout(width, height);
super.setExtent(width, height);
}
public void paint(Graphics graphics)
{
super.paint(graphics);
graphics.clear();
int labelWidth = getFont().getAdvance(getLabel());
graphics.drawRect( labelWidth, 0, getWidth() - labelWidth,getHeight());
graphics.drawText(this.getText() ,3 , 3);
}
}
You can also set font for the editfield.
Regards
Ekansh
09-24-2010 08:26 AM
Great idea to redraw the text yourself . Thanks ! However in the way I do it the text is cropped at half like this :
The code I use is the following
EditField text = new EditField("","",4,style)
{
protected void paint(Graphics g)
{
super.paint(g);
g.clear();
int fieldHeight = getHeight();
int fontHeight = getFont().getHeight();
g.drawText(this.getText() ,5 , (fieldHeight-fontHeight)/2);
}
public int getPreferredHeight()
{
return (int) (getFont().getHeight() * 1.5);
}
public void layout (int width, int height)
{
super.layout (width, height);
if (getExtent().height < getPreferredHeight())
super.setExtent (width, getPreferredHeight());
}
};
06-15-2011 02:03 AM
Using this code for customEditField i m also facing same issue . Text inside Field is not visible relevant to height
pls helpout me how to resolve
06-15-2011 02:23 AM
Ur code:
int fieldHeight = getHeight();
replace with:
int fieldHeight = getFont().getHeight()+5;
06-15-2011 03:31 AM
06-16-2011 05:26 AM
When editfield gets focus to input text then Cursor is not visible so its not clear either to start typing or not
pls help how to show cursor (Stright Vertical line blinking t)