FileDocCategorySizeDatePackage
TextBox.javaAPI DocJ2ME MIDP 2.022041Thu Nov 07 12:02:28 GMT 2002javax.microedition.lcdui

TextBox

public class TextBox extends Screen
The TextBox class is a Screen that allows the user to enter and edit text.

A TextBox has a maximum size, which is the maximum number of characters that can be stored in the object at any time (its capacity). This limit is enforced when the TextBox instance is constructed, when the user is editing text within the TextBox, as well as when the application program calls methods on the TextBox that modify its contents. The maximum size is the maximum stored capacity and is unrelated to the number of characters that may be displayed at any given time. The number of characters displayed and their arrangement into rows and columns are determined by the device.

The implementation may place a boundary on the maximum size, and the maximum size actually assigned may be smaller than the application had requested. The value actually assigned will be reflected in the value returned by {@link #getMaxSize() getMaxSize()}. A defensively-written application should compare this value to the maximum size requested and be prepared to handle cases where they differ.

The text contained within a TextBox may be more than can be displayed at one time. If this is the case, the implementation will let the user scroll to view and edit any part of the text. This scrolling occurs transparently to the application.

If the constraints are set to {@link TextField#ANY} The text may contain line breaks. The display of the text must break accordingly and the user must be able to enter line break characters.

TextBox has the concept of input constraints that is identical to TextField. The constraints parameters of methods within the TextBox class use constants defined in the {@link TextField TextField} class. See the description of input constraints in the TextField class for the definition of these constants. TextBox also has the same notions as TextField of the actual contents and the displayed contents, described in the same section.

TextBox also has the concept of input modes that is identical to TextField. See the description of input modes in the TextField class for more details.

since
MIDP 1.0

Fields Summary
private Form
form
internal form
private TextField
textField
text field object to put on the form
Constructors Summary
public TextBox(String title, String text, int maxSize, int constraints)
Creates a new TextBox object with the given title string, initial contents, maximum size in characters, and constraints. If the text parameter is null, the TextBox is created empty. The maxSize parameter must be greater than zero. An IllegalArgumentException is thrown if the length of the initial contents string exceeds maxSize. However, the implementation may assign a maximum size smaller than the application had requested. If this occurs, and if the length of the contents exceeds the newly assigned maximum size, the contents are truncated from the end in order to fit, and no exception is thrown.

param
title the title text to be shown with the display
param
text the initial contents of the text editing area, null may be used to indicate no initial content
param
maxSize the maximum capacity in characters. The implementation may limit boundary maximum capacity and the actually assigned capacity may me smaller than requested. A defensive application will test the actually given capacity with {@link #getMaxSize()}.
param
constraints see input constraints
throws
IllegalArgumentException if maxSize is zero or less
throws
IllegalArgumentException if the constraints parameter is invalid
throws
IllegalArgumentException if text is illegal for the specified constraints
throws
IllegalArgumentException if the length of the string exceeds the requested maximum capacity

        super(title);

        synchronized (Display.LCDUILock) {
         
            form = new Form(title);
            form.paintDelegate = this;

            if ((TextField.UNEDITABLE & constraints) == TextField.UNEDITABLE) {
                form.paintBorder = BORDER_GRAY;
                // cursorEnabled = false;
            } else {
                form.paintBorder = BORDER_SOLID;
                // cursorEnabled is default to be true;
            }

            textField = new TextField(null, text, maxSize, constraints);
            textField.setBorder(false);

            form.append(textField);
            
        }
    
Methods Summary
voidcallHideNotify(Display d)
Notify this Displayable it is being hidden on the given Display

param
d the Display hiding this Displayable

        super.callHideNotify(d);
        form.callHideNotify(d);
    
voidcallInvalidate(Item src)
Called by the event thread to invalidate the contents of this TextBox

param
src the Item which may have caused the invalidation

        form.callInvalidate(src);
    
voidcallItemStateChanged(Item src)
Called by the event thread to notify an ItemStateChangeListener that an item has changed

param
src the Item which has changed

        form.callItemStateChanged(src);
    
voidcallKeyPressed(int keyCode)
Handle a key pressed event

param
keyCode The key that was pressed

        form.callKeyPressed(keyCode);
    
voidcallKeyReleased(int keyCode)
Handle a key released event

param
keyCode The key that was pressed

        form.callKeyReleased(keyCode);
    
voidcallKeyRepeated(int keyCode)
Handle a key repeated event

param
keyCode The key that was keyRepeated

        form.callKeyRepeated(keyCode);
    
voidcallKeyTyped(char keyCode)
Handle a key typed event

param
keyCode The key that was pressed

        form.callKeyTyped(keyCode);
    
voidcallPaint(Graphics g, java.lang.Object target)
Paint the content of this TextBox

param
g The Graphics object to paint to
param
target the target Object of this repaint

        super.callPaint(g, target);
        form.callPaint(g, target);
    
voidcallShowNotify(Display d)
Notify this Displayable it is being shown on the given Display

param
d the Display showing this Displayable

        super.callShowNotify(d);
        form.callShowNotify(d);
    
voidcommitPendingInteraction()
Called to commit any pending user interaction for the textfield. Override the no-op in Displayable.

            textField.commitPendingInteraction();
    
public voiddelete(int offset, int length)
Deletes characters from the TextBox.

The offset and length parameters must specify a valid range of characters within the contents of the TextBox. The offset parameter must be within the range [0..(size())], inclusive. The length parameter must be a non-negative integer such that (offset + length) <= size().

param
offset the beginning of the region to be deleted
param
length the number of characters to be deleted
throws
IllegalArgumentException if the resulting contents would be illegal for the current input constraints
throws
StringIndexOutOfBoundsException if offset and length do not specify a valid range within the contents of the TextBox

        textField.delete(offset, length);
    
public intgetCaretPosition()
Gets the current input position. For some UIs this may block and ask the user for the intended caret position, and on other UIs this may simply return the current caret position.

return
the current caret position, 0 if at the beginning

        // returns a value relative to the flat input text
        return textField.getCaretPosition();
    
public intgetChars(char[] data)
Copies the contents of the TextBox into a character array starting at index zero. Array elements beyond the characters copied are left unchanged.

param
data the character array to receive the value
return
the number of characters copied
throws
ArrayIndexOutOfBoundsException if the array is too short for the contents
throws
NullPointerException if data is null
see
#setChars

        return textField.getChars(data);
    
public intgetConstraints()
Gets the current input constraints of the TextBox.

return
the current constraints value (see input constraints)
see
#setConstraints

        return textField.getConstraints();
    
public intgetMaxSize()
Returns the maximum size (number of characters) that can be stored in this TextBox.

return
the maximum size in characters
see
#setMaxSize

        return textField.getMaxSize();
    
public java.lang.StringgetString()
Gets the contents of the TextBox as a string value.

return
the current contents
see
#setString

        return textField.getString();
    
public voidinsert(java.lang.String src, int position)
Inserts a string into the contents of the TextBox. The string is inserted just prior to the character indicated by the position parameter, where zero specifies the first character of the contents of the TextBox. If position is less than or equal to zero, the insertion occurs at the beginning of the contents, thus effecting a prepend operation. If position is greater than or equal to the current size of the contents, the insertion occurs immediately after the end of the contents, thus effecting an append operation. For example, text.insert(s, text.size()) always appends the string s to the current contents.

The current size of the contents is increased by the number of inserted characters. The resulting string must fit within the current maximum capacity.

If the application needs to simulate typing of characters it can determining the location of the current insertion point ("caret") using the with {@link #getCaretPosition() getCaretPosition()} method. For example, text.insert(s, text.getCaretPosition()) inserts the string s at the current caret position.

param
src the String to be inserted
param
position the position at which insertion is to occur
throws
IllegalArgumentException if the resulting contents would be illegal for the current input constraints
throws
IllegalArgumentException if the insertion would exceed the current maximum capacity
throws
NullPointerException if src is null

        textField.insert(src, position);
    
public voidinsert(char[] data, int offset, int length, int position)
Inserts a subrange of an array of characters into the contents of the TextBox. The offset and length parameters indicate the subrange of the data array to be used for insertion. Behavior is otherwise identical to {@link #insert(String, int) insert(String, int)}.

The offset and length parameters must specify a valid range of characters within the character array data. The offset parameter must be within the range [0..(data.length)], inclusive. The length parameter must be a non-negative integer such that (offset + length) <= data.length.

param
data the source of the character data
param
offset the beginning of the region of characters to copy
param
length the number of characters to copy
param
position the position at which insertion is to occur
throws
ArrayIndexOutOfBoundsException if offset and length do not specify a valid range within the data array
throws
IllegalArgumentException if the resulting contents would be illegal for the current input constraints
throws
IllegalArgumentException if the insertion would exceed the current maximum capacity
throws
NullPointerException if data is null

        textField.insert(data, offset, length, position);
    
public voidsetChars(char[] data, int offset, int length)
Sets the contents of the TextBox from a character array, replacing the previous contents. Characters are copied from the region of the data array starting at array index offset and running for length characters. If the data array is null, the TextBox is set to be empty and the other parameters are ignored.

The offset and length parameters must specify a valid range of characters within the character array data. The offset parameter must be within the range [0..(data.length)], inclusive. The length parameter must be a non-negative integer such that (offset + length) <= data.length.

param
data the source of the character data
param
offset the beginning of the region of characters to copy
param
length the number of characters to copy
throws
ArrayIndexOutOfBoundsException if offset and length do not specify a valid range within the data array
throws
IllegalArgumentException if data is illegal for the current input constraints
throws
IllegalArgumentException if the text would exceed the current maximum capacity
see
#getChars

        textField.setChars(data, offset, length);
    
public voidsetConstraints(int constraints)
Sets the input constraints of the TextBox. If the current contents of the TextBox do not match the new constraints, the contents are set to empty.

param
constraints see input constraints
throws
IllegalArgumentException if the value of the constraints parameter is invalid
see
#getConstraints

        textField.setConstraints(constraints);
    
public voidsetInitialInputMode(java.lang.String characterSubset)
Sets a hint to the implementation as to the input mode that should be used when the user initiates editing of this TextBox. The characterSubset parameter names a subset of Unicode characters that is used by the implementation to choose an initial input mode. If null is passed, the implementation should choose a default input mode.

See Input Modes for a full explanation of input modes.

param
characterSubset a string naming a Unicode character subset, or null
since
MIDP 2.0

        textField.setInitialInputMode(characterSubset);
    
public intsetMaxSize(int maxSize)
Sets the maximum size (number of characters) that can be contained in this TextBox. If the current contents of the TextBox are larger than maxSize, the contents are truncated to fit.

param
maxSize the new maximum size
return
assigned maximum capacity - may be smaller than requested.
throws
IllegalArgumentException if maxSize is zero or less.
throws
IllegalArgumentException if the contents after truncation would be illegal for the current input constraints
see
#getMaxSize

        return textField.setMaxSize(maxSize);
    
public voidsetString(java.lang.String text)
Sets the contents of the TextBox as a string value, replacing the previous contents.

param
text the new value of the TextBox, or null if the TextBox is to be made empty
throws
IllegalArgumentException if text is illegal for the current input constraints
throws
IllegalArgumentException if the text would exceed the current maximum capacity
see
#getString

        textField.setString(text);
    
public voidsetTicker(Ticker ticker)
Sets a ticker for use with this Displayable, replacing any previous ticker. If null, removes the ticker object from this Displayable. The same ticker may be shared by several Displayable objects within an application. This is done by calling setTicker() with the same Ticker object on several different Displayable objects. If the Displayable is actually visible on the display, the implementation should update the display as soon as it is feasible to do so.

The existence of a ticker may affect the size of the area available for Displayable's contents. If the application adds, removes, or sets the ticker text at runtime, this can dynamically change the size of the content area. This is most important to be aware of when using the Canvas class.

param
ticker the ticker object used on this screen
since
MIDP 2.0
see
#getTicker

        super.setTicker(ticker);

        // We override this method from Displayable to set the ticker
        // on our internal Form which is doing all our rendering

        form.setTicker(ticker);
    
public voidsetTitle(java.lang.String s)
Sets the title of the Displayable. If null is given, removes the title.

If the Displayable is actually visible on the display, the implementation should update the display as soon as it is feasible to do so.

The existence of a title may affect the size of the area available for Displayable content. If the application adds, removes, or sets the title text at runtime, this can dynamically change the size of the content area. This is most important to be aware of when using the Canvas class.

param
s the new title, or null for no title
since
MIDP 2.0
see
#getTitle

        super.setTitle(s);

        // We override this method from Displayable to set the title
        // on our internal Form which is doing all our rendering

        form.setTitle(s);
    
public intsize()
Gets the number of characters that are currently stored in this TextBox.

return
the number of characters

        // returns a value relative to the display text including formatting
        return textField.size();