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

Format text in a RichTextField

by BlackBerry Development Advisor ‎02-16-2010 01:06 PM - edited ‎09-16-2010 02:34 PM (6,952 Views)



This article applies to the following:

  • BlackBerry® Java Development Environment (JDE)
  • BlackBerry Wireless Handhelds™ based on Java™



A RichTextField can be used to present selectable, formatted text to a user. Multiple fonts, font sizes, and font formatting (for example, Bold, Italic, Underlined) can be used in a single RichTextField.


Note: All BlackBerry handhelds or BlackBerry simulators support multiple fonts and font formatting.



 * CheckBox sample with an EditField
 * Please note that not all BlackBerry handhelds or BlackBerry simulators
 * support the bold or italic formatting used in this sample.

package com.samples.richTextFieldSample;

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 RichTextFieldSample extends UiApplication

    public static void main(String[] args)
            RichTextFieldSample theApp = new RichTextFieldSample();

    public RichTextFieldSample()
        //Define the screen.
        MainScreen mainScreen;

        //Defint the RichTextField.
        RichTextField rtField;

        //An array of fonts to be passed to the RichTextField.
        Font fonts[] = new Font[3];

        //A list of offsets of character positions of the string contained
        //in the RichTextField where font appearance will change.
        //The first value in the offset array should be 0,
        //the starting character of the string and the last value should
        //be the last character position in the string.
        int[] offset = new int[4];

        //An array of indexes that align with the offset values and indicate
        //the index of the font array of the font to use starting at an offset.
        byte[] attribute = new byte[3];

        //Get three instances of the default font.
        //On plain, one bold and one bold and italic.
        fonts[0] = Font.getDefault();
        fonts[1] = Font.getDefault().derive(Font.BOLD);
        fonts[2] = Font.getDefault().derive(Font.BOLD | Font.ITALIC);

        //The string of text we will display in the RichTextField.
        String richText = "This is how you create text with formatting!!!";

        //Offset will start a character 0 of the string.
        offset[0] = 0;
        //At character position 0 we want to use the font at
        // position 2 of the font array (bold and italic).
        attribute[0] = 2;

        //Change to the default (non-bold, non-italic) font at
        //the 4th character position.
        offset[1] = 4;
        attribute[1] = 0;

        //Change to bold font at the 33rd character position.
        offset[2] = 33;
        attribute[2] = 1;

        //Add the last character position to the offset.
        offset[3] = richText.length();

        //Instantiate mainScreen.
        mainScreen = new MainScreen();

        //Set the title of mainscreen.
        mainScreen.setTitle(new LabelField
          ("RichTextFieldSample Sample",
            LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH));

        //Instantiate rtField.
        rtField = new RichTextField
          (richText, offset, attribute, fonts,

        //Add the RichTextField to the MainScreen.

        //Push the MainScreen onto the stack.


Users Online
Currently online: 44 members 1,791 guests
Please welcome our newest community members: