Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Setting label fonts in Java ME

From Wiki
Jump to: navigation, search

This code example demonstrates how to set the font for a label using the MIDP 2.0 API.

Article Metadata
Code ExampleTested with
Devices(s): Nokia N95, Nokia N96, Nokia 5800 XpressMusic, Nokia 6131, Nokia C3-01, Nokia Asha 306, Nokia E7-00
Created: olkazmin (26 Feb 2009)
Last edited: hamishwillee (09 Aug 2013)



The Font object instance can be retrieved only by using static methods of the Font class. It is not possible to create a font, but a font with specific attributes can be requested:

// The "monospace" font face.
// The "proportional" font face.
// The "system" font face.
// The "large" font size.
// The "medium" font size.
// The "small" font size.
// The bold style constant.
// The "italic" style constant.
// The plain style constant.
// Displays text as underlined.

Alternatively, you can use one of the following specifiers:

// Font specifier used by the implementation to draw text input by a user.
// Default font specifier used to draw Item and Screen contents.

In this example, the Font object is retrieved by using font attributes. For that purpose the method Font.getFont is used. It takes three arguments:

  • font face id;
  • font style id;
  • font size id.

The second argument, font style id, can hold STYLE_PLAIN, or a combination of STYLE_BOLD, STYLE_ITALIC, and STYLE_UNDERLINED, using bit 'OR' operation.
The Font object instance can be set to any object that allows it. In this case, the font for StringItem is changed using the StringItem.setFont method.

For more information, see API documentation.

Source file:

* Sets font for stringToFormat accordingly to the attributes selected via
* fontFaceChoiceGr, fontSizeChoiceGr and fontStyleChoiceGr choice groups.

private void applyFontAttr() {
Font myFont = null;
try {
// Setting font for stringToFormat
myFont = Font.getFont( getSelectedFontFace(),
getSelectedFontSize() );
} catch ( IllegalArgumentException e) {
printString( "Error. No such font!!!" );
printString( "Getting default font ..." );
myFont = Font.getDefaultFont();
printString( "Applying font to label ..." );
stringToFormat.setFont( myFont );
printString( "Font Applied!" );


When the snippet is executed, the main form with a text field is displayed. Pressing the 'Start' softkey will bring another form with a label to the foreground. This label has the default font at the beginning. To change a font, press the 'Font' softkey to display the font attribute controls. Select the font attributes you want, and press the 'OK' softkey to see the label with the selected font attributes.

To get back to the main form, press the 'Back' softkey.

Supplementary material

This code snippet is part of the stub concept, which means that it has been patched on top of a template application in order to be more useful for developers. The version of the Java ME stub application used as a template in this snippet is v1.1.

This page was last modified on 9 August 2013, at 05:26.
89 page views in the last 30 days.