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
New Developer
Posts: 133
Registered: ‎12-31-2009
My Device: Not Specified

Problem in setting color and background in LabelField

Hello friends,

 

I am trying to set background color to LabelField . I am using following code for this.

 

 

 

LabelField serverUriLabel = new LabelField("UserName:",LabelField.USE_ALL_WIDTH)


		{

		public void paint (Graphics g)
		{
		g.pushContext(getExtent(), 0, 0);
		g.setBackgroundColor(Color.CYAN);
		g.clear();
		g.popContext();
		g.setColor(Color.BLACK);
		super.paint(g);


		}
		};




		add(serverUriLabel);
		RegiPhoneNo = new EditField("","");
		RegiPhoneNo.setFont(editFont);


		add(RegiPhoneNo);
		add(new LabelField(""));

		LabelField emailUriLabel = new LabelField("Password:",LabelField.USE_ALL_WIDTH)
		{

		public void paint (Graphics g)
		{
		g.pushContext(getExtent(), 0, 0);
		g.setBackgroundColor(Color.CYAN);
		g.clear();
		g.popContext();
		g.setColor(Color.BLACK);
		super.paint(g);


		}
		};

		add(emailUriLabel);
		emailPhoneNo = new EditField("","");
		add(emailPhoneNo);
		add(new LabelField(""));

		LabelField RegistrUriLabel = new LabelField("Name:",LabelField.USE_ALL_WIDTH)
		{

		public void paint (Graphics g)
		{
		g.pushContext(getExtent(), 0, 0);
		g.setBackgroundColor(Color.CYAN);
		g.clear();
		g.popContext();
		g.setColor(Color.BLACK);
		super.paint(g);
		// this.invalidate();

		}
		};

 

 

 I am getting the first LabelField "serverUriLabel" properly with cyan color background and black text on it. But the next all LabelFields are not with color background and black text on it. The background color is not set to cyan color and text is in black ( I guess this black color is default black of system and not due to my setColor () ). I am not getting what is the problem.

 

Somebody please help me.

 

 

Thank you

 

 

 

 

Developer
Posts: 656
Registered: ‎02-03-2010
My Device: Z10 LE
My Carrier: Rogers

Re: Problem in setting color and background in LabelField

Have you tried it without the Push/Pop context?


Steven Kader at JaredCo
   Follow me on Twitter    |     Mipixi.com :  Web App for Photo Sharing
New Developer
Posts: 133
Registered: ‎12-31-2009
My Device: Not Specified

Re: Problem in setting color and background in LabelField

Hello JaredCo,

 

Thank you for your reply.

 

I will try with your sugesstion and let you know.

 

 

Thank you.

 

 

 

 

 

 

Developer
Posts: 1,006
Registered: ‎05-22-2009
My Device: Not Specified

Re: Problem in setting color and background in LabelField

This is customLabelField component,i hope it will help you

 

import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.LabelField;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author rakesh_2
 */
public class MyLabelField extends LabelField
{
    int borderwidth=this.getPreferredWidth();
    int borderheight=this.getPreferredHeight();
    int frclr,bkclr=0,bderclr;
    MyLabelField(String s)
    {
        super(s);
        frclr=Color.BLACK;
        bkclr=Color.WHITE;
        bderclr=Color.WHITE;
    }
     MyLabelField(String s,long lg)
    {
        super(s,lg);
        frclr=Color.BLACK;
        bkclr=Color.WHITE;
        bderclr=Color.WHITE;
    }
    public void setforegroundcolor(int color)
    {
        frclr=color;
    }
    public void setbackgroundcolor(int color)
    {
        bkclr=color;
    }
     public void setbordercolor(int color)
    {
        bderclr=color;
    }
      public void setBorder(int b,int c)
    {
        borderwidth=b;
        borderheight=c;
        bderclr=Color.BLACK;
    }
    public void paint(Graphics g)
    {
        g.setColor(bderclr);
        g.drawRect (0, 0, borderwidth,borderheight);
        g.setColor(frclr);
              
        /*if(bderclr!=Color.WHITE)
        {
            g.drawRect (0, 0, borderwidth,borderheight);
        }*/
        
        super.paint(g);
    }
    

  
    public void paintBackground(Graphics g)
    {
        g.setBackgroundColor(bkclr);
        g.clear();
    }
    
    protected void onFocus(int direction)
    {
      frclr=Color.VIOLET;
      bkclr=Color.BEIGE;
      this.invalidate();
    }
     
    protected void onUnfocus()
    {
      frclr=Color.BLACK;
      this.invalidate();
    }
   
    public void layout(int arg,int arg1)
    {
        super.layout(borderwidth,borderheight);  
        //super.layout(Display.getWidth()/2,this.getPreferredHeight());      //Used for determining the length of component
        this.setExtent(borderwidth,borderheight);   //Used for setting the bounddary of component   
    }
    
}