Search code examples
blackberryblackberry-eclipse-pluginblackberry-jde

Comma separation in the Text Field in Blackberry


in my application i have a Custom text box with BasicEditField.FILTER_NUMERIC. When the user enter the value in the field the comma should be added to the Currency format .

EX:1,234,567,8.... like this.

In my code i tried like this.

protected boolean keyUp(int keycode, int time) {
    String entireText = getText();
    if (!entireText.equals(new String(""))) {
        double val = Double.parseDouble(entireText);

        String txt = Utile.formatNumber(val, 3, ",");// this will give the //comma separation format 
        setText(txt);// set the value in the text box
    }
    return super.keyUp(keycode, time);
}

it will give the correct number format... when i set the value in the text box it will through the IllegalArgumentException. I know BasicEditField.FILTER_NUMERIC will not allow the charector like comma(,)..

How can i achieve this?


Solution

  • I tried this way and it works fine...

    public class MyTextfilter extends TextFilter {
    private static TextFilter _tf = TextFilter.get(TextFilter.REAL_NUMERIC);
    
    public char convert(char character, int status) {
        char c = 0;
    
        c = _tf.convert(character, status);
        if (c != 0) {
            return c;
        }
    
        return 0;
    }
    
    public boolean validate(char character) {
        if (character == Characters.COMMA) {
            return true;
        }
    
        boolean b = _tf.validate(character);
        if (b) {
            return true;
    
        }
    
        return false;
    }
    }
    

    and call like this

    editField.setFilter(new MyTextfilter());