03-07-2011 04:07 PM
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());
}
}
Solved! Go to Solution.
03-07-2011 05:29 PM
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);
??
03-07-2011 05:40 PM - edited 03-07-2011 05:58 PM
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:
and after finishing typing, I get this (Perfect!) but only after clicking another field
[
And unless I comment out this line from the paint method
super.paint(graphics); // Comment this out
I get this
]
03-08-2011 09:10 AM
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.
03-08-2011 12:44 PM
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.
03-08-2011 12:54 PM
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.
03-08-2011 01:16 PM
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?
03-08-2011 01:20 PM
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)
03-08-2011 03:34 PM
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 ![]()