Namespaces

Variants
Actions

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 over the next few weeks. 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
CompatibilityArticle
Created: olkazmin (26 Feb 2009)
Last edited: hamishwillee (09 Aug 2013)

Contents

Overview

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.
Font.FACE_MONOSPACE
// The "proportional" font face.
Font.FACE_PROPORTIONAL
// The "system" font face.
Font.FACE_SYSTEM
// The "large" font size.
Font.SIZE_LARGE
// The "medium" font size.
Font.SIZE_MEDIUM
// The "small" font size.
Font.SIZE_SMALL
// The bold style constant.
Font.STYLE_BOLD
// The "italic" style constant.
Font.STYLE_ITALIC
// The plain style constant.
Font.STYLE_PLAIN
// Displays text as underlined.
Font.STYLE_UNDERLINED


Alternatively, you can use one of the following specifiers:

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


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: SettingLabelFont.java

    /**
* 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(),
getSelectedFontStyle(),
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!" );
}

Postconditions

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.
151 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×