Original function: A multiline input field with a fixed height and width border that can be scrolled vertically.
Improvement: The input box draws a rounded border, with a different border color for focus and non-focus states.
Original: How to-create a custom field using attributes of the other UI objects
Import Net.rim.device.api.ui.Color;
Import Net.rim.device.api.ui.Graphics;
Import Net.rim.device.api.ui.Manager;
Import Net.rim.device.api.ui.component.EditField;
Import Net.rim.device.api.ui.container.VerticalFieldManager;
/*
* 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 of our object
public TextBoxField (int width, int height) {
This call to super would help keep the object
Super (Manager.no_vertical_scroll);
Managerwidth = width;
Managerheight = height;
VFM'll allow scrolling within the object
Verticalfieldmanager VFM = new Verticalfieldmanager (manager.vertical_scroll);
EditField = new EditField () {
public void Paint (Graphics g) {
This invalidation'll 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 ());
Draw the rounded corners of different colors according to focus or not
int oldcolour = G.getcolor ();
if (This.isfocus ()) {
G.setcolor (color.red); Border color of onfocus
G.drawroundrect (0, 0, getwidth (), GetHeight (), 15, 15);
} else {
G.setcolor (Color.gray); Border color of Onunfocus
G.drawroundrect (0, 0, getwidth (), GetHeight (), 15, 15);
}
G.setcolor (Oldcolour);
}
If This call to Sublayout is made by the system then
Both parameters would is 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'll force the height of the object
& nbsp; //where the above super.sublayout () call to
//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);
}
protected void onfocus (int direction) {
Super.onfocus (direction);
Editfield.setcursorposition (Editfield.gettextlength ());
Invalidate (); Force paint Draw Border
}
protected void Onunfocus () {
super.onunfocus ();
invalidate (); //force Paint Draw Border
}