org.j4me.ui.components
Class Label

java.lang.Object
  extended by org.j4me.ui.components.Component
      extended by org.j4me.ui.components.Label

public class Label
extends Component

Displays a string.

If the string is wider than the screen, it is wrapped at the first non-alphanumeric character. The wrapped text is trimmed so that leading spaces do not appear. There are several special markup strings for controling spacing:

The string is displayed using the theme's font and color unless overridden by the setFont and setFontColor methods.

Override this class to display strings in a new way. For example a font backed by images of the characters can be used by re-implementing paint, getWidth, and getHeight. A good way to generate font images is with the AngelCode Bitmap Font Generator.


Field Summary
 
Fields inherited from class org.j4me.ui.components.Component
container, HIGHLIGHTED_BORDER_WIDTH
 
Constructor Summary
Label()
          Constructs an empty label.
Label(java.lang.String text)
          Constructs a label.
 
Method Summary
static java.lang.String[] breakIntoLines(javax.microedition.lcdui.Font font, java.lang.String string, int width)
          Breaks string apart at line breaks.
 javax.microedition.lcdui.Font getFont()
           
 int getFontColor()
           
 int getHeight()
           
 java.lang.String getLabel()
           
protected  int[] getPreferredComponentSize(Theme theme, int viewportWidth, int viewportHeight)
          Returns the dimensions of the label for the current string.
protected  void invalidate()
          Signals that the Component's size needs to be updated.
protected  void paintComponent(javax.microedition.lcdui.Graphics g, Theme theme, int width, int height, boolean selected)
          Paints this label component.
 void setFont(javax.microedition.lcdui.Font font)
           
 void setFontColor(int color)
           
 void setLabel(java.lang.String text)
           
 
Methods inherited from class org.j4me.ui.components.Component
acceptsInput, getHorizontalAlignment, getPreferredSize, getScreen, getWidth, getX, getY, hideNotify, isShown, keyPressed, keyReleased, keyRepeated, paint, paintRect, pointerDragged, pointerPressed, pointerReleased, repaint, setHorizontalAlignment, showNotify, visible
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Label

public Label()
Constructs an empty label. The test should be set using setLabel.


Label

public Label(java.lang.String text)
Constructs a label.

Parameters:
text - is what the label displays.
Method Detail

invalidate

protected void invalidate()
Description copied from class: Component
Signals that the Component's size needs to be updated. This method is intended to be called by subclassed components to force layout of the component to change. A call to this method will return immediately, and it will cause the container's layout algorithm to run at some point in the future.

Overrides:
invalidate in class Component
See Also:
Component.invalidate()

getLabel

public java.lang.String getLabel()
Returns:
The text this label will display.

setLabel

public void setLabel(java.lang.String text)
Parameters:
text - is the string to display. If null the label will be hidden.

getFont

public javax.microedition.lcdui.Font getFont()
Returns:
The Font used to display the label. If null the theme's font will be used.
See Also:
Theme.getFont()

setFont

public void setFont(javax.microedition.lcdui.Font font)
Parameters:
font - is the font to use for this label. If null the theme's font will be used.
See Also:
Theme.getFont()

getFontColor

public int getFontColor()
Returns:
The font color used to display the label. If negative the theme's font will be used.
See Also:
Theme.getFontColor()

setFontColor

public void setFontColor(int color)
Parameters:
color - is the font color used to display the label. If negative the theme's font will be used.
See Also:
Theme.getFontColor()

paintComponent

protected void paintComponent(javax.microedition.lcdui.Graphics g,
                              Theme theme,
                              int width,
                              int height,
                              boolean selected)
Paints this label component.

Specified by:
paintComponent in class Component
Parameters:
g - is the Graphics object to be used for rendering the item.
theme - is the application's theme. Use it to get fonts and colors.
width - is the width, in pixels, to paint the component.
height - is the height, in pixels, to paint the component.
selected - is true when this components is currently selected and false when it is not.
See Also:
Component.paintComponent(Graphics, Theme, int, int, boolean)

getPreferredComponentSize

protected int[] getPreferredComponentSize(Theme theme,
                                          int viewportWidth,
                                          int viewportHeight)
Returns the dimensions of the label for the current string. The width is equal to the width of the longest line of text. The height is equal to the sum of all the lines including line breaks between paragraphs.

Specified by:
getPreferredComponentSize in class Component
Parameters:
theme - is the application's Theme.
viewportWidth - is the width of the viewable area, in pixels, the component can use.
viewportHeight - is the height of the viewable area, in pixels, the component can use.
Returns:
A array with two elements where the first is the width of the component in pixels and the second is the height.
See Also:
Component.getPreferredComponentSize(Theme, int, int)

getHeight

public int getHeight()
Overrides:
getHeight in class Component
Returns:
The height, in pixels, for this component.

breakIntoLines

public static java.lang.String[] breakIntoLines(javax.microedition.lcdui.Font font,
                                                java.lang.String string,
                                                int width)
Breaks string apart at line breaks. The returned array has strings that can be written using font in fewer than width pixels.

The string will be split apart at non-alphanumeric characters. However, if a string is too long and cannot be broken nicely, it will be split at the last alphanumeric character that fits the line width.

Newline characters ('\n') are automatically made into line breaks. An additional empty line, represented by a null string in the returned array, is added. It can then be painted as a space to indicate a new paragraph.

Parameters:
font - is the Font used to write string.
string - is the text to split. If null then null will be returned.
width - is the maximum number of pixels wide each string part can be.
Returns:
An array of parts of string broken apart at line breaks.