Question

I am a newbie to Blackberry, I was just trying out some sample apps in Blackberry. I tried to create a login page. In that, when I tried to change the width of the text field, the text field became invisible.

The below is part of the code to create the TextField.

super(Field.FIELD_VCENTER);
......
t_username = new TextField()
{
    public void layout(int width, int height)
    {
        super.layout(500, 30);
        setExtent(500, 30);
    }
};
t_username.setMaxSize(10);
t_username.setBorder(BorderFactory.createSimpleBorder(new XYEdges(1,1,1,1),Border.STYLE_SOLID));

I tried to create a border to check where it is coming or not, but couldn't find it. PFB, the snapshot:

enter image description here

Was it helpful?

Solution

Overriding the TextField#layout() method

public void layout(int width, int height)
{
    super.layout(500, 30);
    setExtent(500, 30);
}

is one way that you can set a text field's width. So, I think there's something else going wrong here.

1) Possibly, when you changed your code, your mistakenly removed the call to

add(t_username);

you don't show us where you actually add that field, so if you're not calling add(t_username) somewhere else, it's not going to be visible. Fields must be added to a Manager or Screen to be visible.

2) Perhaps some other code you've written (but not shown) is attempting to do something with a Graphics object. For example, if you're overriding a paint(Graphics) method in another field, you may be changing a color (e.g. graphics.setColor(Color.WHITE)) and not remembering to reset the original color. Possibly, your text field is there, but it's the same color as its background. If this was happening, though, you could still see the text field cursor when you give that field focus. I simply can't tell from your screenshot.

A Better Way

Normally (but not always), it should be the responsibility of the Manager/Screen that contains the field to determine its size. I think it's poor encapsulation to have most fields set their own width (although there are exceptions to this). I would recommend using setMargin() and the USE_ALL_WIDTH flag to set a reasonable width for this text field:

public class TextFieldScreen extends MainScreen {

   private TextField t_username;

   public TextFieldScreen() {
      super(Field.FIELD_VCENTER);

      HorizontalFieldManager row = new HorizontalFieldManager();
      LabelField label = new LabelField("Username");
      label.setMargin(new XYEdges(2, 0, 2, 10));
      row.add(label);

      t_username = new TextField(TextField.USE_ALL_WIDTH);
      t_username.setMaxSize(10);
      t_username.setBorder(BorderFactory.createSimpleBorder(new XYEdges(1,1,1,1), Border.STYLE_SOLID));
      t_username.setMargin(new XYEdges(2, 10, 2, 10));
      row.add(t_username);

      add(row);
   }
}

One benefit of this solution, compared to hard-coding a width of 500, is that if your app supports portrait/landscape rotation, the code above will correctly adjust the field width as the screen width changes. If you hard-code the field width, the field will be too wide when the device rotates to portrait.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top