Tuesday, April 14, 2009

Support : A Field’s font is displayed incorrectly when set in the paint method

Support Problem

The Field.setFont(Font font) method should be used to set or change the font used by any field in the BlackBerry API set. A font should not be set or changed within the Field’s paint method. The following code example will NOT work:

LabelField myField = new LabelField("Hi there.")
{
   public void paint(Graphics graphics)
   {
      graphics.setFont(graphics.getFont().derive(Font.BOLD));
      super.paint(graphics);
   }
}

Resolution

The Field’s setFont method should be used in the following way:

LabelField myfield = new LabelField("Hi there.")
myField.setFont(myFont);

This allows for the font used within a specific field to be set. To change a font for the entire application, the Font.setDefaultFont(Font font) method should be used.

Note: This does not mean that the Graphics.setFont method should not be used. If you are performing custom drawing actions using the Graphics class on a custom Screen or Field, the setFont method can be used to change the font that is being drawn. Graphics.setFont should not be used to change the font being drawn by existing fields contained within the BlackBerry API set. Under this circumstance, the Field.setFont method should be used.

No comments:

Post a Comment

Place your comments here...