Determining the dimensions of a field

When you draw fields on the screen of your application, there are several methods in the Field class that you can use to determine the dimensions of a field. Depending on which method you use, you might receive different values for the width and height of the field. It's important to use the method that is most appropriate in a particular situation to make sure that your fields are drawn properly.

The getWidth() and getHeight() methods retrieve the amount of space that the field uses within the manager that contains the field. The values that are returned from these methods include any area that is required for the padding and border of the field. A field cannot use the padding area or border area when it draws its own content, but it is important for the field's manager to retrieve the width and height of the field with these areas included so that the manager can arrange the fields without overlapping.

The getContentWidth() and getContentHeight() methods retrieve the amount of space that the content area of the field uses. The values that are returned from these methods do not include the space that is used by the padding and border. When a field paints itself using paint(), the Graphics object that the field receives is configured with the origin in the upper-left corner of the content area. The clip area, which is the area that the field is allowed to draw in, is specified as the width and height of the content area. The field can draw only in this area.

The getPreferredWidth() and getPreferredHeight() methods are designed to retrieve the space that a custom field should use if the field has an unlimited amount of space available. By default, these methods return 0. When you create a custom field, you should override these methods to return the preferred width and height of the custom field. These values are considered suggestions, and may not be used by a manager when it lays out the fields that it contains.

When you are using any of the methods that are listed above to determine the dimensions of a field, you should not assume that the values that are returned from getWidth() and getHeight() are the same as those returned from getContentWidth() and getContentHeight(). If padding or a border is added to a field, these values will be different. Similarly, you should not assume that the values that are returned from getWidth() and getHeight() are the same as those returned from getPreferredWidth() and getPreferredHeight().

Next topic: Managers
Previous topic: Margin

Was this information helpful? Send us your comments.