ErrorContentTextField

The ErrorContentTextField is a similar to the TextField component, with the addition that it is capable of displaying an error message, if the user has attempted to type an erroneous value. Showing or hiding the error message, listening for typing input and defining the condition that triggers the display of the error message, is entirely left to the developer.

Figure 1. An ErrorContentTextField consists of a TextField instance and optionally an error message

The ErrorContentTextField can be initialized by passing the text of its TextField and a constraint for the type of the text, as arguments to its constructor:

ErrorContentTextField errorText = new ErrorContentTextField("", TextArea.ANY);

It is allowed to pass the empty String as the first argument to the constructor. All the operations allowed on a LWUIT TextField can be performed on the ErrorContentTextField as well, by accessing its TextField instance as follows:

TextField textInstance = errorText.getTextField();

Adding or removing the error message is done with the addErrorContent and removeErrorContent methods respectively.

The following snippet demonstrate how to define an ErrorContentTextField that displays an error message if 5 characters or more have been given as input:

Display.init(this);
form = new Form("Error text field");
errorText = new ErrorContentTextField("", TextArea.ANY);
TextField textInstance = errorText.getTextField();
textInstance.setHint("Hint");
textInstance.addDataChangeListener(new DataChangedListener() {
    public void dataChanged(int arg0, int arg1) {
        if (errorText.getText().length() >= 5) {
            errorText.addErrorContent("Error: 5 of more characters have been given");
        }
        else {
            errorText.removeErrorContent();
        }
    }
});
form.addComponent(errorText);
form.show();

Last updated 17 October 2013

Back to top

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×