How to capitalize initial word in lwuit textfield?

149 Views Asked by At

I have tried my textfield to have the very first letter to be capitalized with the following code :-

Form f = new Form();
TextField firstname = new TextField();
firstname.setConstraint(TextField.INITIAL_CAPS_SENTENCE);
f.addComponent(firstname);
f.show();

But this is not working.

What am I missing here? Can anybody suggest a correct way to achieve it?

Note : I am using LWUIT 1.5

Edited

This is how I finally did it with the help of Shai

public void insertChars(String c) {
    super.insertChars(c); //To change body of generated methods, choose Tools | Templates.
    if(super.getText()!=null && super.getText().length()>0){      
        super.setText((super.getText().substring(0,1).toUpperCase())+super.getText().substring(1, super.getText().length()));
    }else{
        super.setText(super.getText());
    }
}
1

There are 1 best solutions below

1
On BEST ANSWER

Are you using a qwerty J2ME device by any chance? If so this won't work since this only applies to the native side of the things.

You need to derive TextField and override insertChar() to implement this.