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
Developer
Posts: 98
Registered: ‎12-29-2009
My Device: Bold
My Carrier: Rogers
Accepted Solution

API 4.2--Background Color of EditField--Possible to Change?

Hi,

 

Is it possible to change the background color of an EditField when the EditField has focus?  I was able to get such a feature to work with API 4.6 using the BackgroundFactory, but I noticed that this component is not available in 4.2.

 

I would appreciate any help!

 

Thanks!

 

Developer
Posts: 98
Registered: ‎12-29-2009
My Device: Bold
My Carrier: Rogers

Re: API 4.2--Background Color of EditField--Possible to Change?

[ Edited ]

Maybe this will work?

 

 

public void paint(Graphics focuson)
            {
                XYRect xy = focuson.getClippingRect();
               focuson.fillRect(xy.x,xy.y,xy.width,xy.height);
               focuson.setColor(Color.Blue);

            }

public void paint(Graphics focusoff)
            {
                XYRect xy = focusoff.getClippingRect();
                focusoff.fillRect(xy.x,xy.y,xy.width,xy.height);
                focusoff.setColor(Color.White);            }


 

 

Then, I can simply call super.paint(focusoff) when the EditField does not have focus, and super.paint(focuson) when the EditField has focus.

 

Any thoughts anyone?

Developer
Posts: 98
Registered: ‎12-29-2009
My Device: Bold
My Carrier: Rogers

Re: API 4.2--Background Color of EditField--Possible to Change?

Anyone?

Developer
Posts: 19,621
Registered: ‎07-14-2008
My Device: Not Specified

Re: API 4.2--Background Color of EditField--Possible to Change?

I generally do this by having an _isFocused boolean in the Field, setting that and invalidating in onFocus, resetting and invalidating on onUnfocus, then in  paint, fill the Field much as you have done, but using the Field's extent rather than the clipping rectangle.  So the logic is in paint.  I think if you search the forum, you will find others have done the same thing. 

Developer
Posts: 19,621
Registered: ‎07-14-2008
My Device: Not Specified

Re: API 4.2--Background Color of EditField--Possible to Change?

I see zany has posted this same solution in your other Thread.  Please can we only have one Thread?  If you do start another Thread then close off the old Thread and point people at the new one.  That way the people who answer these questions don't waste there time, as I have just done. 

http://supportforums.blackberry.com/t5/Java-Development/EditField-Background-Change-Color/td-p/43644...

Developer
Posts: 98
Registered: ‎12-29-2009
My Device: Bold
My Carrier: Rogers

Re: API 4.2--Background Color of EditField--Possible to Change?

Thank you for your help, Peter (as always).

 

I apologize for posting another forum.  That was quite by accident.  I managed to find what I thought was a solution (which turned out to be false) and then posted it to another forum.

 

I tried Zaney's solution, but the variable _bcColor is not apart of EditField???

 

 

Developer
Posts: 16,637
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: API 4.2--Background Color of EditField--Possible to Change?

then: make it so!

(read: create a variable of this name)

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 98
Registered: ‎12-29-2009
My Device: Bold
My Carrier: Rogers

Re: API 4.2--Background Color of EditField--Possible to Change?

Hi Simon,

 

Thank you for your assistance.

 

I don't quite follow your suggestion.

 

"Then:  make it so!"--what do you mean?

"(Read: create variable of this name)--what do you mean by this?"

 

Could you be more specific?

 

Developer
Posts: 19,621
Registered: ‎07-14-2008
My Device: Not Specified

Re: API 4.2--Background Color of EditField--Possible to Change?

In my solution, I suggested you added an

_infocus boolean

which is set and reset in onFocus and onUnfocus respectively and then tested in paint to decide what background colour to paint.

 

In zany's solution he added another variable

_bgColor integer

and in paint, you can paint the background with the color in _bgColor.

 

I hope that that is clear now. 

Developer
Posts: 98
Registered: ‎12-29-2009
My Device: Bold
My Carrier: Rogers

Re: API 4.2--Background Color of EditField--Possible to Change?

Okay, I think I understand now.

 

What you're suggesting is this.

 

1)  Establish the paint method--as per my previous post.

2) Set a boolean variable in the onFocus and onUnFocus methods, and call the Paint method.

3)  Within the Paint method If the boolean is true then the EditField color will be X, and if the boolean is false then the EditField background color will be Y.

 

*4) Zany has suggested that I establish a _bgcolor variable within the onFocus and onUnFocus methods.  The variable is changed within these focus methods, and then the Paint method is called.  The Paint method will refer to the _bgcolor variable.

 

5) invalidate() must be called in the onFocus and onUnFocus methods.

 

Is this correct?