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
Developer
Posts: 137
Registered: ‎06-01-2009
My Device: Not Specified
Accepted Solution

LabelField not able to change font size

Regardless the font size I set, It seems no affect and the font size does not change!!. I can change color but not font. What is wrong in code below ?

 

public class FMHorizSingleCont extends HorizontalFieldManager {

 

private LabelField lbDesc;

 

  public FMHorizSingleCont () {

 

     lbDesc = new LabelField(title, LabelField.USE_ALL_WIDTH){

   // Font font = Font.getDefault().derive(Font.PLAIN,6,Ui.UNITS_pt);

       protected void paint(Graphics g) {
      Font font = Font.getDefault().derive(Font.PLAIN,6,Ui.UNITS_pt);

      g.setFont(font);

      g.setColor(Color.
BLACK);

      invalidate();

      super.paint(g);

     }

 };

 

add(lbDesc);

Developer
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: LabelField not able to change font size

Set font below the paint() method.

Like:

lbDesc = new LabelField(title, LabelField.USE_ALL_WIDTH) { protected void paint(Graphics g) { //Font font = Font.getDefault().derive(Font.PLAIN,6,Ui.UNITS_pt); //g.setFont(font); g.setColor(Color.BLACK); invalidate(); super.paint(g); } }; Font font = Font.getDefault().derive(Font.PLAIN,6,Ui.UNITS_pt); lbDesc.setFont(font);

 

Regards

Bikas

 

 

Highlighted
Developer
Developer
Posts: 137
Registered: ‎06-01-2009
My Device: Not Specified

Re: LabelField not able to change font size

It works. Thanks.
Developer
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: LabelField not able to change font size

You are welcome. Smiley Happy
Developer
Posts: 19,631
Registered: ‎07-14-2008
My Device: Not Specified

Re: LabelField not able to change font size

I have only just noted this Thread and wish to correct the code given as the solution. 

 

The paint method given in the solution was as follows:

 

    protected void paint(Graphics g)
    {  
       g.setColor(Color.BLACK);
       invalidate();
       super.paint(g);
    }

 

(I have removed the comments.)

In this case the invalidate() is unnecessary and will cause the Blackberry extra work - it will in fact paint the LabelField a number of times.  invalidate() is an instruction to the system to repaint a Field at some stage in the (near) future.  If you give the system this instruction in paint, then every time you paint the Field you are telling the system to repaint it.  This will go on forever running down the battery on your user's phone. 

 

So this code should really be:

    protected void paint(Graphics g)
    { 
       g.setColor(Color.BLACK);
       super.paint(g);
    }

 

Now the default color is Black, so in fact this be removed also, leaving:

 

    protected void paint(Graphics g)
    {  
       super.paint(g);
    }

 

In which case the entire paint() method can be removed, since it is not doing anything useful.

 

And just for the record, it is good practice, when you change something that might impact other places, to make sure your processing doesn't have side effects.  So for example, if you do need to change the color of some text, try using code like this:

 

    protected void paint(Graphics g)
    { 
int currCol = g.getColor()

try {

       g.setColor(<new color>);
       super.paint(g);

} finally {

g.setColor(currColor);

}
    }