Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
Posts: 17
Registered: ‎10-28-2008
My Device: Not Specified

Re: fixedwidth buttonField cannot center text

Here is a further improvement to truncate long button names more tidily. If you like this solution please give it KUDOS.

 

Haemish

 

public class CustomButtonField extends ButtonField
{
    String m_text;
    public CustomButtonField(String a_text)
    {
        super("", ButtonField.CONSUME_CLICK | ButtonField.NEVER_DIRTY | Field.FIELD_HCENTER);
        m_text = a_text;
    }
    
    public int getPreferredWidth()
    {
        return Display.getWidth();
    }
    
    protected void paint(Graphics a_graphics)
    {
        super.paint(a_graphics);
        a_graphics.drawText(m_text, 0, 0, DrawStyle.HCENTER, getContentWidth());
    }
}

Developer
Posts: 264
Registered: ‎10-05-2010
My Device: Not Specified

Re: fixedwidth buttonField cannot center text

[ Edited ]

I had some painting problems and wanted to control the width so I simplified it further and normalized the variables.

 

Can someone please point out why I need to subtract 40 from the width to get it right? I'm sure it's a margin somewhere but I can't figure out how to calculate it. Hard coding it is not the answer...

 

I call it like this:

 

 

btStart = new FixedButtonField("Start", Display.getWidth()/2);
btStop  = new FixedButtonField("Stop",  Display.getWidth()/2);

 

 

 

public class FixedButtonField extends ButtonField
{
    String _text;
    int _width;
    
    public FixedButtonField(String text, int width) {
        super(text, ButtonField.CONSUME_CLICK | ButtonField.NEVER_DIRTY | Field.FIELD_HCENTER);
        _text = text;
        _width = width;
    }
    
    public int getPreferredWidth(){
        return _width-40;
        
    }
    
    protected void paint(Graphics g) {
        super.paint(g);
    }
}

 

 

 

Highlighted
Developer
Posts: 62
Registered: ‎05-30-2011
My Device: 9200
My Carrier: Etisalat

Re: fixedwidth buttonField cannot center text

I used this class in an application and wrote the following lines

but the button never appear and when I extend from the buttonfield it appears but the label never appears

 

btn = new myButton("Button1");

btn.setSize(100, 100);

btn.setLabel("labeeeeeel");

btn.setAlignment(myButton.ALIGNMENT_CENTER);

add(btn);

 

So can you help