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: 53
Registered: ‎01-12-2011
My Device: BOLD 9780
My Carrier: Bharti Airtel
Accepted Solution

How to Change TextColor of LabelField

Hi

I want to change text color of LabelField

I tried following code - 

 

_labelField = new LabelField("Email"){
            public void paint(Graphics graphics){
               
                graphics.setColor(Color.WHITE);
                graphics.clear();
                super.paint(graphics);
            }
        };

but it shows me white box instead of LabelField
I dont want to change its background color

Is it possible without using CustomLabelField??

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

Re: How to Change TextColor of LabelField

overwrite paintbackground

----------------------------------------------------------
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: 19,628
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to Change TextColor of LabelField

Not 100% sure what the problem is here.

 

To clear a Field you normally use code like

 

g.setbackgroundColor(...);

g.clear();

 

this is normally in the paintBackground but can also be in paint.

 

By setting

g.setColor(..)

you are setting the default color for the following operations.

 

But in this case you clear the background without setting the background color, which will probably clear it to white, as I think that is the default  background color,  So then if you set the color to white, you will write white text on a white background - i.e. your text will be invisible.

 

Use this information and try other combinations of colors and clears and paintbackgrounds until you get a combination that works for you. 

Developer
Posts: 53
Registered: ‎01-12-2011
My Device: BOLD 9780
My Carrier: Bharti Airtel

Re: How to Change TextColor of LabelField

Hi

Thanx for the Suggestion

But i have myself found solution by playing

with paintground and paint method as suggested by you (Simon_hain)

 

 

Here is my solution

_emailLabel = new LabelField("Email",LabelField.USE_ALL_WIDTH | LabelField.NON_FOCUSABLE){
            public void paintBackground(Graphics g){
                g.setBackgroundColor(Color.WHITE);
                g.setGlobalAlpha(0);
                g.clear();
                super.paint(g);
            }
            public void paint(Graphics g){
                g.setColor(Color.WHITE);
                g.setGlobalAlpha(255);
                super.paint(g);
            }
        };

Thanx for the suggestion :smileyhappy:

Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: How to Change TextColor of LabelField

That's an extremely confused code. You  definitely should not call super.paint from paintBackground. In fact, with your code super.paint is called twice each time the field needs painting. The first time it does nothing (globalAlpha of 0 means it is fully transparent and does not affect display). Besides, g.clear will behave differently under different OS versions with global alpha less than 255.

 

Your initial problem was that you tried to paint white text on white background, making it invisible. So choose your background and foreground colors to be in contrast and then do something like this:

// suppose you have your background and foreground colors in
// int _backColor and int _foreColor, respectively
protected void paintBackground(Graphics g) {
  int prevBgColor = g.getBackgroundColor();
  g.setBackgroundColor(_backColor);
  g.clear();
  g.setBackgroundColor(prevBgColor);
}

protected void paint(Graphics g) {
  int prevColor = g.getColor();
  g.setColor(_foreColor);
  super.paint(g);
  g.setColor(prevColor);
}

It's easier, clearer and will work no matter what OS you are using.


 

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
New Contributor
Posts: 8
Registered: ‎06-02-2013
My Device: non-blackberry user
My Carrier: AXIS Indonesia

Re: How to Change TextColor of LabelField

Hi. This is work for me. But I have image as the background, and if I use this code, my LabelField's background has a solid color. How to make the LabelField's background transparent so my background image can show beautifully. THX

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

Re: How to Change TextColor of LabelField

Please create your own thread for your question.
Don't use the paintBackground method if you don't want to paint your background.
----------------------------------------------------------
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