Eric,
I just let the width of my fields adjust themselves based on the data being displayed. That way, nothing is cut off if I made the width too small, and there's no wasted space if I made the width too large.
Along the way, I've also played around with creating a custom field by passing an additional parameter to set a fixed width. To make this work properly, you also have to specify that the field will display using a mono spaced font (ie, system).
Rather than try to explain it all, take a look at the following code. The _labelWidth and setExtent call is what makes this work. I started with the CustomFieldButton example and came up with this.
Code:
public class CustomButtonField extends Field implements DrawStyle {
private String _label;
private String text = "W";
private int _shape;
private Font _font;
private int _labelHeight;
private int _labelWidth;
private long _style;
// Constructs a rectangular button with specified label, shape, and style
public CustomButtonField(String label, long style, int fixWidth) {
super(style);
_style = style;
_label = label;
_font = getFont();
_labelHeight = _font.getHeight();
if (fixWidth == 0) {
_labelWidth = _font.getAdvance(_label);
} else {
for (int i=1; i<fixWidth; i++) {
text = text + "W";
}
_labelWidth = _font.getAdvance(text);
}
}
/* Method that draws the focus indicator for this button and
* inverts the inside region of the shape.
*/
protected void drawFocus(Graphics graphics, boolean on) {
graphics.invert(1, 1, getWidth() - 2, getHeight() - 2);
}
/* Returns the label. */
public String getLabel() {
return _label;
}
/* Sets the label. */
public void setLabel(String label) {
_label = label;
_labelWidth = _font.getAdvance(_label);
updateLayout();
}
/* Retrieves the preferred width of the button. */
public int getPreferredWidth() {
return _labelWidth + 8;
}
/* Retrieves the preferred height of the button. */
public int getPreferredHeight() {
return _labelHeight + 4;
}
/**
* Lays out this button's contents.
* This field's manager invokes this method during the layout
* process to instruct this field to arrange its contents, given an
* amount of available space.
**/
protected void layout(int width, int height) {
// Update the cached font in case it has been changed.
_font = getFont();
_labelHeight = _font.getHeight();
// Calculate width.
width = Math.min( width, getPreferredWidth() );
// Calculate height.
height = Math.min( height, getPreferredHeight() );
// Set dimensions.
setExtent( width, height );
}
/*
* Redraws this button. The field’s manager invokes this method during the
* repainting process to instruct this field to repaint itself
*/
protected void paint(Graphics graphics) {
int textX, textY, textWidth;
int w = getWidth();
//Use light grey lines to draw border of rectangle
int orgColor = graphics.getColor();
graphics.setColor(0x007f7f7f);
graphics.drawRect(0, 0, w, getHeight());
textX = 4;
textY = 2;
textWidth = w - 6;
//Return original color
graphics.setColor(orgColor);
graphics.drawText(_label, textX, textY,
(int)( _style & ( DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK )),
textWidth );
}
}
Again, to set a fixed width I mostly borrowed from the CustomButtonField example and added the section to set the width of a number of W characters as the extent.
Hope this helps.