Setting label fonts in Java ME
This code example demonstrates how to set the font for a label using the MIDP 2.0 API.
Article Metadata
Code Example
Tested with
Compatibility
Article
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:
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.
- The patched, executable application that can be used to test the features described in this snippet is available for download at Media:SettingLabelFont.zip.
- You can view all the changes that are required to implement the above-mentioned features. The changes are provided in unified diff and colour-coded diff (HTML) formats in Media:SettingLabelFont.diff.zip.
- For general information on applying the patch, see Using Diffs.
- For unpatched stub applications, see Example app stubs with logging framework.


(no comments yet)