Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Create custom fields

by BlackBerry Development Advisor ‎02-16-2010 12:06 PM - edited ‎09-16-2010 02:34 PM (6,034 Views)

Summary

 

This article applies to the following:

  • BlackBerry® Java Development Environment (JDE)
  • BlackBerry Wireless Handhelds™ based on Java™

Background

 

A field is a container that is designed to hold and lay out a particular type of data for display in the user interface (UI). For example, a date field has the ability to store a date and display it in several formats, such as 2002/01/17. Although there are several useful field types defined in the BlackBerry UI, you can create your own. The BlackBerry UI provides you with the functionality to extend the basic Field to create any type of field.

Procedure

 

To create a custom field, you must first extend Field and implement at least the following methods:

 

 

protected abstract void layout(int width, int height)

 

This method lays out the fields contents. It ultimately calls setExtent(int x, int y), which tells the field how much space it requires on the screen.

 

 

protected abstract void paint(Graphics graphics)

 

The code sample above is the paint method, which is called by the screen when your field must be repainted. In this method, you make the necessary calls to the graphics object to paint your field's contents. For example, if you are creating a text field you might need to call graphics.drawText("text", xPosition, yPosition) to draw the text.

 

In addition, you might want to extend the following methods, depending on the functionality and layout of your field:

 

 

public int getPreferredWidth()

 

The above code sample is a method called by the layout manager to determine the amount of horizontal space to allocate to the field. This is also used to determine proper horizontal scrolling. For text fields, the horizontal space required is the sum of the width of each character plus the buffer space between them. There is a method in the Font class to help calculate the required width.

 

 

public int getPreferredHeight()

 

The above code sample is a method called by the layout manager to determine the amount of vertical space to allocate to the field. This is also used to determine proper vertical scrolling.

 

If you want to change your field's ability to accept focus, overwrite the method public boolean isFocusable() returning True. This allows the field to accept focus. Returning False does not allow the field to accept focus.

 

The following code samples are from a custom text field that can contain several (non-editable) separate pieces of text. Each text segment is allocated a certain amount of space and is separated by a given amount of buffer space. This is similar to a table layout.

 

 

class MultiElementTextField extends Field { // we must extend field
   String[] _texts; // an array of text segments
   int[] _spaces; // an array containing the amount of space
                       // allocated to each text segment
   int_buffer; // an integer value which represents
                       // the amount of buffer space between
                       // each text segment
   /* Constructor sets up each of the data arrays
      and the buffer value */
   public MultiElementTextField(Object[] data, int buffer, long style)
       throws IllegalArgumentException {
       super(style); // a call to super, this sets up the
                       // particular style of the field.
       int n = data.length;
       if ((n % 2) != 0) {
          throw new IllegalArgumentException("Invalid number of elements");
       } else {
           _texts = new String[n / 2];
           _spaces = new int[n / 2];
       }
       for (int i = 0; i < n ; i = i + 2) {
           _texts[i / 2] = (String)data[i];
           _spaces[i / 2] = ((Integer)data[i+1]).intValue();
       }
       _buffer = buffer;
   }
   public boolean isFocusable() {
       // this field can accept the focus, return true
       return true;
   }
   /* we use layout to determine the extent of the field,
      we ultimately call setExtent(int, int) with the total field size */
   public void layout(int width, int height) {
       Font font = getFont();
       int _fieldHeight = font.getHeight();
       int _fieldWidth = 0;
       int n = _spaces.length;
       for (int i = 0; i < n; ++i) {
          _fieldWidth += _spaces[i];
           if (i < n - 1) {
               _fieldWidth += _buffer;
           }
       }
       setExtent(_fieldWidth, _fieldHeight);
   }
   /* paint draws the text objects to the screen using the
      graphics object passed in. Here we are using the values
      from buffer and spaces[] to determine the location and
      size of each segment of text.We use the ELLIPSIS style
      to handle text that is too long for its allocated space
      by replacing any characters that do not fit with ... */
   public void paint(Graphics graphics) {
       int xpos = 0;
       int n = _spaces.length;
       for (int i = 0; i < n; ++i) {
           graphics.drawText(_texts[i],xpos,0,graphics.ELLIPSIS,_spaces[i]);
           xpos += _spaces[i];
           if (i < n - 1) {
               xpos += _buffer;
           }
       }
   }

 


Additional Information

 

For more information and sample codes on creating a custom field, see the BlackBerry Application Developer Guide.

Contributors
Users Online
Currently online: 4 members 2,546 guests
Please welcome our newest community members: