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

Change fonts in a BlackBerry application

by Retired ‎02-12-2010 03:22 PM - edited ‎09-16-2010 03:46 PM (8,344 Views)



This article applies to the following:

  • BlackBerry® Java™ Development Environment (JDE)
  • BlackBerry wireless devices based on Java



The BlackBerry device supports multiple fonts in various sizes. These fonts can be used in custom BlackBerry applications and can be changed during runtime. The number of fonts available on a device depends on the model and the BlackBerry Device Software version installed.


The following example illustrates how to use fonts in a custom BlackBerry application and change the font during runtime:



 * Font change sample

package com.samples.fontChange;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;

public final class FontChange extends UiApplication
    private Font font;

    //Get all fonts available on the BlackBerry
    private FontFamily fontFamily[] = FontFamily.getFontFamilies();

    public static void main(String[] args)
            FontChange theApp = new FontChange();
    public FontChange()
        MainScreen mainScreen;

        FontChangeListener fontChangeListener = new FontChangeListener();

        //Set the current font to the first in the font list.
        font = fontFamily[0].getFont(FontFamily.SCALABLE_FONT, 10);


        mainScreen = new MainScreen();

        //Set the title of the mainscreen.
        LabelField title = new LabelField("Font Test Example",
            LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);

        ObjectChoiceField pickFont = new ObjectChoiceField("Select a font:",
            fontFamily, 0);


        mainScreen.add(new SeparatorField());

        //RichTextField that displays text in the specified colour.
        RichTextField fontText = new RichTextField
            ("The quick red fox jumps over the lazy brown dog.")
            public void paint(Graphics graphics)
                //Change the colour of the text in the RichTextField to green.

        //Display the main screen


    //Create a custom FieldChangeListener that will change the current font
    //to what is selected in the ObjectChangeField.

    class FontChangeListener implements FieldChangeListener
        public void fieldChanged(Field field, int context)
            //If the ObjectCoiceField has changed
            if (field instanceof ObjectChoiceField)
                //Get the new font selected in the ObjectChoiceField and set the
                //current font to match what is selected.

                ObjectChoiceField choiceField = (ObjectChoiceField)field;
                font = fontFamily[choiceField.getSelectedIndex()].getFont


Users Online
Currently online: 15 members 641 guests
Please welcome our newest community members: