-1

I had an custom editfield

public class Custom_EditField extends EditField {
int width, row;

Custom_EditField(long style, int width, int row) {
    super(style);
    this.width = width;
    this.row = row;
}

protected void layout(int width, int height) {
    width = this.width;
    height = this.row;
    super.layout(width, Font.getDefault().getHeight() * row);
    super.setExtent(width, Font.getDefault().getHeight() * row);
}

public int getPreferredHeight() {
    return Font.getDefault().getHeight() * row;
}

public int getPreferredWidth() {
    return width;
}

public void paint(Graphics graphics) {
    super.paint(graphics);
    graphics.setBackgroundColor(Color.GRAY);
    graphics.clear();
    graphics.setColor(Color.BLACK);
    int labelWidth = getFont().getAdvance(getLabel());
    graphics.drawRect(labelWidth, 0, getWidth() - labelWidth, getHeight());
    graphics.drawText(this.getText(), 0, 0);
}
}

When I type a full line of word in the editfield, it will cause and error. It seem like cannot auto go to next line.

CRUSADER
  • 5,486
  • 3
  • 28
  • 64
Alan Lai
  • 1,094
  • 7
  • 18
  • 41
  • 1
    "it will cause an error" May you explain which error you get? Also a [SSCCE](http://pscode.org/sscce.html) would be helpful. – Howard Jul 02 '12 at 05:21
  • `0:21:21.476: App Error 104 0:21:21.478: Uncaught: StackOverflowError` – Alan Lai Jul 02 '12 at 07:22

1 Answers1

1

The arguments to the layout method in BlackBerry UI are maximums, and your custom code makes no attempt to honor those maximums when setting the field extent. This will cause problems with your layout. Also, the paint() method is not the best place to alter drawing for text fields, because it doesn't understand text wrapping. If you want to alter how the text is drawn, but after the wrapping has been performed, you want to override drawText instead.

This is approximately what you want, but you will need to do some more tweaking to make it work the way you expect:

protected void layout(int maxWidth, int maxHeight) {
    super.layout(maxWidth, Math.min(maxHeight, Font.getDefault().getHeight() * row));
    super.setExtent(maxWidth, Math.min(maxHeight, Font.getDefault().getHeight() * row));
}

public int drawText(Graphics graphics,
                int offset,
                int length,
                int x,
                int y,
                DrawTextParam drawTextParam) {
    graphics.setBackgroundColor(Color.GRAY);
    graphics.clear();
    graphics.setColor(Color.BLACK);
    int labelWidth = getFont().getAdvance(getLabel());
    graphics.drawRect(labelWidth, 0, getWidth() - labelWidth, getHeight());
    graphics.drawText(this.getText().substring(offset, offset + length), x, y);
}
Michael Donohue
  • 11,776
  • 5
  • 31
  • 44