Q:

Custom Dialog box in blackberry

How to create custom Dialogue box and add listeners to the buttons in the dialogue box in blackberry

A:

public class CustomDialog extends Screen implements FieldChangeListener 
{
private ButtonField okButton;
public void fieldChanged(Field field, int context) 
{
    if (field == okButton) 
    {
        close();

    }
}
public CustomDialog(String message) 
{
    super(new VerticalFieldManager(), Screen.DEFAULT_CLOSE);

    add(new LabelField("Search Error",LabelField.FIELD_HCENTER));
    add(new LabelField(""));
    add(new LabelField(message,LabelField.FIELD_HCENTER));
    add(new LabelField(""));        
    okButton = new ButtonField("OK",ButtonField.FIELD_HCENTER);
    okButton.setChangeListener(this);
    add(okButton);
} 
protected void paintBackground(Graphics graphics) 
{
    graphics.setColor(Color.GRAY);
    graphics.fillRoundRect(0, 0, getWidth(), getHeight(), 12, 12);
    graphics.setColor(Color.BLACK);
    graphics.drawRoundRect(0, 0, getWidth(), getHeight(), 12, 12);
}  
protected void sublayout(int width, int height) 
{
    layoutDelegate(width - 80, height - 80);
    setPositionDelegate(10, 10);
    setExtent(width - 60, Math.min(height - 60, getDelegate().getHeight() + 20));
    setPosition(30, (height - getHeight())/2);
}
}

