07-12-2010 02:24 PM
Hi All,
I am using the following code for TextBoxField:
http://na.blackberry.com/eng/devjournals/resources
now i am trying to change TextBoxField border color when it focused. But could not able to get.
need some help on this.
Thanks,
Solved! Go to Solution.
07-12-2010 02:59 PM - edited 07-12-2010 03:48 PM
Ask yourself: where is that border painted? If you look carefully, you'll find this:
public void paint(Graphics g) {
super.paint(g);
g.drawRect(0, 0, getWidth(), getHeight());
}
That g.drawRect paints the border. How do you change its color, you ask? You use Graphics.setColor() for that. So, you change that to:
public void paint(Graphics g) {
super.paint(g);
int prevColor = g.getColor();
g.setColor(myBorderColor);
g.drawRect(0, 0, getWidth(), getHeight());
g.setColor(prevColor);
}
(please observe that saving of the previous color and restoring it after you've done using your custom one).
That myBorderColor should be a member variable in your TextBoxField, which you change in your onFocus() and your onUnfocus() calls. If you do it there, don't forget to invalidate() your whole TextBoxField after changing that color.
Now, if you use that TextBoxField, be advised that it is an example of some extremely careless programming - and I don't use those words lightly. The culprit is this piece of code:
editField = new EditField(){
public void paint(Graphics g) {
getManager().invalidate();
super.paint(g);
}
};
The highlighted line causes the manager to repaint its fields, which, in turn, calls the same paint() again - and again, and again, creating a kind of a soft infinite loop on event (UI) thread. Since the rationale behind that invalidation is to preserve the border during vfm's scrolling, the right approach would be to catch that scrolling event and invalidate the manager there. Use ScrollChangeListener for that. Something like this:
VerticalFieldManager vfm =
new VerticalFieldManager
(Manager.VERTICAL_SCROLL);
vfm.setScrollListener(new ScrollChangeListener() {
void scrollChanged(Manager mgr, int horizontal, int vertical) {
mgr.getManager().invalidate();
}
});
I doubt you need to go through all that trouble as the border has nothing to do with the inner Manager scrolling, though one needs to test that.
Also notice that the original code would not refresh that border anyway, as the Manager of the editField is vfm, not the whole TextBoxField! And invalidating vfm is not enough to make its parent Manager to repaint the border.
Edit: Just debugged and had success with this:
public class TextBoxField extends VerticalFieldManager {
int myWidth;
int myHeight;
VerticalFieldManager vfm = new VerticalFieldManager(VERTICAL_SCROLL | VERTICAL_SCROLLBAR | USE_ALL_WIDTH | USE_ALL_HEIGHT);
EditField editField = new EditField("", "", EditField.DEFAULT_MAXCHARS, FOCUSABLE);
TextBoxField(int width, int height) {
super(NO_VERTICAL_SCROLL);
myWidth = width;
myHeight = height;
add(vfm);
vfm.add(editField);
}
public String getText() {
return editField.getText();
}
protected void sublayout(int w, int h) {
int actWidth = Math.min(myWidth, w);
int actHeight = Math.min(myHeight, h);
super.sublayout(actWidth - 2, actHeight - 2); // Leave room for border
setPositionChild(vfm, 1, 1); // again, careful not to overlay the border
setExtent(actWidth, actHeight);
}
protected void paint(Graphics g) {
super.paint(g);
int prevColor = g.getColor();
g.setColor(Color.BLACK);
g.drawRect(0, 0, getWidth(), getHeight());
g.setColor(prevColor);
}
}
Again, change g.setColor() the way you see fit.
07-13-2010 02:52 PM
Hi arkadyz,
I really appreciate for the way u have explained and thanks for sharing the snippet. I have tried your code snippet's for border change.
I have some issues the border get changed on focus but i couldn't able to see the cursor in textbox and can't able to type any thing inside textbox or may be it get typed but not been painted on screen. If i remove this methods i can see the cursor and type keywords but not border change.
I have added these two methods
protected void onFocus(int direction) {
if(direction == 0 || direction == 1 || direction == -1) {
myBorderColor = Color.BLUE;
}
}
protected void onUnfocus() {
myBorderColor = Color.BLACK;
}
I think it has to do with some paint method of editField. i i tried to figure out but not succeeded. can u please help
07-13-2010 03:00 PM - edited 07-13-2010 03:58 PM
amsiddh wrote:
Hi arkadyz,
I really appreciate for the way u have explained and thanks for sharing the snippet. I have tried your code snippet's for border change.
I have some issues the border get changed on focus but i couldn't able to see the cursor in textbox and can't able to type any thing inside textbox or may be it get typed but not been painted on screen. If i remove this methods i can see the cursor and type keywords but not border change.
I have added these two methods
protected void onFocus(int direction) { if(direction == 0 || direction == 1 || direction == -1) { myBorderColor = Color.BLUE; } } protected void onUnfocus() { myBorderColor = Color.BLACK; }I think it has to do with some paint method of editField. i i tried to figure out but not succeeded. can u please help
Where did you add those methods? The only focusable field you have is editField, so you need them there. Besides, I've already mentioned that you have to invalidate() your whole TextBoxField for it to be repainted. So, editField creation should look like this:
EditField editField = new EditField("", "", EditField.DEFAULT_MAXCHARS, FOCUSABLE) {
protected void onFocus(int direction) {
myBorderColor = Color.BLUE;
TextBoxField.this.invalidate();
}
protected void onUnfocus() {
myBorderColor = Color.BLACK;
TextBoxField.this.invalidate();
}
};
Your myBorderColor should be a private int member in TextBoxField itself.
Edit: first of all, add super.onFocus(direction) and super.onUnfocus() as the first lines of the corresponding methods. Second, I've just discovered that onUnfocus() is never called! Talk about BlackBerry framework! I've tested it under simulator (Bold 9000), but GUI stuff usually performs the same on the real device.
What's interesting is that I've used those methods many times and they always work. Strange... When I have time to investigate more, I'll find out what's going wrong and post the results here.
07-13-2010 03:14 PM
Hi arkadyz,
Actually i was adding those methods into TextBoxField instead off EditField.
Really thanks from my heart for your quick reply's and explanation's. Great man
![]()
you deserve for Kudos....![]()
Thanks,
amsiddh