How To - Create a custom field using attributes of other UI objects
Last Updated: 27 March 2009 | |
Article Number: DB-00737 |
Summary
This article applies to the following:
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);
}
}
Keywords
Custom field, field, interface, VerticalFieldManager, TextField, TextBox