Search code examples
arraysjava-melcdui

J2ME TextField in a Vector?


I want to have a Form that can Add or Delete TextFields.

I was so far creating an array and resizing (actually copying original array to a new, larger array), then deleting all form elements, and adding everything again + this new array of TextFields

but I think this will slow down the program when there are many TextFields
Adding TextFileds to the Vector is not working. When it is about to add TextField to the Form,

form.append(vector.elementAt(i));

it says that the element is not it.

method Form.append(Item) is not applicable
  (actual argument Object cannot be converted to Item by method invocation conversion)
method Form.append(Image) is not applicable
  (actual argument Object cannot be converted to Image by method invocation conversion)
method Form.append(String) is not applicable
  (actual argument Object cannot be converted to String by method invocation conversion)

Should I countinue with resizing arrays, or is there a better way?


Solution

  • According to Form documentation "The items contained within a Form may be edited using append, delete, insert, and set methods." And you also have a get method, so I don't think you need a Vector at all. Lets say you have:

    
        Form form = new Form("Multiple fields");
    
        // If you want to add a new TextField
        form.append(new TextField("label", "text", 10/*maxSize*/, TextField.ANY));
    
        // if you want to delete the last TextField:
        form.delete(form.size() - 1);
    
        // to iterate at all fields:
        for (int i = 0; i < form.size(); i++) {
            TextField textField = (TextField) form.get(i);
        }