How to display text in multiline using customized label field in Blackberry?

667 views Asked by At

How to display text in multi-line using customized label field in Blackberry? When I use following code, I can display the label with desired font size, however the problem is when the extent is limited in width the text gets cut off and not displayed entirely , also rest of the label part is not displayed on next line.

Any Help here is appreciated.

Following is my code for customized label field.

public class GrayBgLabelField extends LabelField {  

    private int width = Display.getWidth();
    private int height = 40;
    private String label;   
    private Font font;

    public GrayBgLabelField(){
        super();
    }

    public GrayBgLabelField(String label, int fieldWidth){
        super(label, 0 );
        this.label = label;
        width = fieldWidth;     
    }

    public GrayBgLabelField(String label, int fieldWidth , long style){
        super( label, style );
        this.label = label;
        width = fieldWidth;     
    }

    public GrayBgLabelField(String label, int fieldWidth , int fieldHeight){

        this(label,fieldWidth);
        this.label = label;
        height = fieldHeight;
    }

    public GrayBgLabelField(String label, int fieldWidth , int fieldHeight, long style){

        super( label, style );
        this.label = label;
        width = fieldWidth;
        height = fieldHeight;
    }

    protected void layout( int maxWidth, int maxHeight)
    {         
        super.layout( width, height);  
        setExtent( width, height);
    }

    protected void paint(Graphics g) {

        if(font !=null){
            g.setFont(font);
        }

        if (label.length() != 0) {          
            g.drawText(label, width/30, height/4);
        }
        g.setColor(Color.BLACK);

    }

    public void setFont(int f){
        font = this.getFont().derive(f);        
    }

    public void setFont(Font font){
        this.font = font;
    }

    protected void paintBackground(Graphics g) {
        int oldColor = g.getColor();
        try {
            g.setColor(0xF5F6F8);  // Gray-DDDDDD color code.
            g.fillRect(0, 0, getWidth(), getHeight());          
        } finally {
            g.setColor(oldColor);
        }
    }
}
1

There are 1 answers

1
AudioBubble On BEST ANSWER

Override methods which control width and height of your custom field.

Here is the tutorial: "How to create a custom field"

And when you change your label field text, invalidate it via invalidate() method, to redraw field contents on the screen.