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 a custom field using attributes of other UI objects

by Retired ‎02-12-2010 03:49 PM - edited ‎09-16-2010 02:35 PM (3,128 Views)

Summary

 

This article applies to the following:

  • BlackBerry® smartphones based on Java® technology

Details

 

Using Java technology, you can create a custom field that has a tailored behavior by combining and changing the elements of other UI objects.

 

The following example shows how to combine the VerticalScrollManagers and EditFields to create a new TextObjectField item. The newly created item is a hybrid of the two items used to create it, allowing for a text box with dimensions set in its constructor - a fixed width and vertical scrolling.

 

 

/*
 * TextBoxField.java
 */

//Extend VerticalFieldManager to help control scrolling and
//to set a fixed width/height for our field
public class TextBoxField extends VerticalFieldManager {
    //Define some variables to be used
    //in the class
    private int managerWidth;
    private int managerHeight;
    private EditField editField;

    //Pass in the fixed height and width for our object
    public TextBoxField(int width, int height) {
        //This call to super will help keep the object in place
        super(Manager.NO_VERTICAL_SCROLL);
        managerWidth = width;
        managerHeight = height;

        //vfm will allow scrolling within the object
        VerticalFieldManager vfm = new VerticalFieldManager(Manager.VERTICAL_SCROLL);

        editField = new EditField(){
            public void paint(Graphics g) {
            //This invalidation will help keep the border clean
            //while scrolling
            getManager().invalidate();
            super.paint(g);
        }
    };

    vfm.add(editField);
    add(vfm);
    }


    public void paint(Graphics g) {
        super.paint(g);
        //Draw a rectangle around out TextBoxField
        g.drawRect(0, 0, getWidth(), getHeight());
    }

    //If this call to sublayout was made by the system then
    //both parameters would be passed with a value of 0.
    //This check and adjustment keeps the fixed properties
    //maintained.
    public void sublayout(int width, int height) {

        if (managerWidth == 0) {
            managerWidth = width;
        }
        if (managerHeight == 0) {
            managerHeight = height;
        }
        super.sublayout(managerWidth, managerHeight);
        //Force the extent of our manager.
        //This will force the height of the object
        //where the above super.sublayout() call will
        //set the width.
        setExtent(managerWidth,managerHeight);
    }


    //The following two methods allows users of the
    //TextBofField read and set its contents.
    public String getText() {
        return editField.getText();
    }

    public void setText(String text) {
        editField.setText(text);
    }
}

 

Contributors
Users Online
Currently online: 27 members 557 guests
Please welcome our newest community members: