Visit Sponsor

Written by 6:17 pm Core Java

Customizing LabelField in BlackBerry Java

Overview

In BlackBerry Java development, LabelField is a fundamental UI component used to display static text on a screen. By default, it renders simple text, but developers often need to customize its appearance (such as font, color, alignment, and style).

This page explains how to extend and modify a LabelField to achieve customized text rendering and layout behavior using BlackBerry Java APIs.

LabelField and UI Customization in BlackBerry Java

BlackBerry user interfaces are built using the Field and Manager hierarchy (similar to layout managers in other platforms). Fields are added to screens and managers to control layout and drawing behavior. LabelField is a basic field subclass that draws text.

By default, LabelField uses system fonts and default colors. To customize appearance, developers override methods like paint() and set custom fonts using the Font class.

Default LabelField Usage

import net.rim.device.api.ui.component.LabelField;

LabelField label = new LabelField("Default Text");
add(label);

This renders plain text using the default system font. Customization requires subclassing.

Customizing Font and Style

To change font characteristics (such as bold style, size, or typeface), you must call setFont() with a derived font object.

import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.FontFamily;
import net.rim.device.api.ui.component.LabelField;

LabelField label = new LabelField("Custom Text");
FontFamily fontFamily = FontFamily.forName("BBGlobalSans");
Font customFont = fontFamily.getFont(Font.PLAIN, 18);
label.setFont(customFont);
add(label);

This example sets a specific font and size. Use FontFamily.forName() carefully and catch exceptions if the font name is unknown.

Overriding paint() for Color and Background

To change text color, background color, or draw additional graphics, subclass LabelField and override the paint(net.rim.device.api.ui.Graphics) method.

Example — custom label with color:

LabelField coloredLabel = new LabelField("Custom Colored Label") {
    protected void paint(Graphics graphics) {
        graphics.setColor(Color.BLUE);   // Set text color
        super.paint(graphics);           // Draw text
    }
};
add(coloredLabel);

This approach controls exactly how the label draws itself. You can also fill a background rectangle before drawing text for further styling.

Custom LabelField With Multiple Style Properties

You can override paint() to combine multiple style effects (color, background, font):

CustomLabelField myLabel = new CustomLabelField("Styled Label", Field.FIELD_HCENTER);

class CustomLabelField extends LabelField {
    public CustomLabelField(String text, long style) {
        super(text, style);
        Font font = Font.getDefault().derive(Font.BOLD, 14);
        setFont(font);
    }

    protected void paint(Graphics graphics) {
        graphics.setColor(Color.WHITE);                // Background
        graphics.fillRect(0, 0, getWidth(), getHeight());
        graphics.setColor(Color.RED);                  // Text color
        super.paint(graphics);
    }
}
add(myLabel);

This custom field:

  • Centers text horizontally
  • Sets bold font style
  • Draws colored background then text

Alignment and Style Flags

BlackBerry fields support style flags for alignment and layout behavior:

Style FlagBehavior
Field.FIELD_HCENTERHorizontally centers the label
Field.FIELD_RIGHTAligns text to the right
LabelField.ELLIPSISTruncates with ellipsis if text is long
LabelField.USE_ALL_WIDTHUses full width available

Use these flags when constructing the custom field:

LabelField label = new LabelField("Centered", Field.FIELD_HCENTER | LabelField.USE_ALL_WIDTH);

Additional Customization Options

  • Use layout managers (like VerticalFieldManager or HorizontalFieldManager) to position label fields with other UI components.
  • Override layout() in custom fields when precise sizing or positioning logic is needed.
  • Handle focus and interaction by combining style flags (e.g., Field.FOCUSABLE).

When to Use Custom LabelField

Customizing LabelField is appropriate when:

  • You need specific fonts, sizes, or text effects
  • You need custom backgrounds behind text
  • Default UI elements lack required styling capability

For complex UI animations or layouts, consider using a higher-level UI framework or creating custom fields with overridden layout logic.

Visited 26 times, 1 visit(s) today
Close