private String _label;
private int _shape;
private Font _font;
private int _labelHeight;
private int _labelWidth;
}
4.
Implement constructors to define a label, shape, and style of the custom button.
public CustomButtonField(String label) {
this(label, RECTANGLE, 0);
}
public CustomButtonField(String label, int shape) {
this(label, shape, 0);
}
public CustomButtonField(String label, long style) {
this(label, RECTANGLE, style);
}
public CustomButtonField(String label, int shape, long style) {
super(style);
_label = label;
_shape = shape;
_font = getFont();
_labelHeight = _font.getHeight();
_labelWidth = _font.getAdvance(_label);
}
5.
Implement
layout()
to specify the arrangement of field data. Perform the most complex calculations in
layout()
instead of in
paint()
. The manager of the field invokes layout() to determine how the field arranges its contents in the
available space. In the following code sample, we invoke
Math.min()
to return the smaller of the specified width and
height and the preferred width and height of the field. We then invoke
Field.setExtent(int,int)
to set the
required dimensions for the field.
protected void layout(int width, int height) {
_font = getFont();
_labelHeight = _font.getHeight();
_labelWidth = _font.getAdvance(_label);
width = Math.min( width, getPreferredWidth() );
height = Math.min( height, getPreferredHeight() );
setExtent( width, height );
}
6.
Implement
getPreferredWidth()
, using the relative dimensions of the field label to make sure that the label does
not exceed the dimensions of the component. In the following code sample, we use a switch block to determine the preferred
width based on the shape of the custom field. For each type of shape, we use an IF statement to compare dimensions and
determine the preferred width for the custom field.
Development Guide
UI components
24