java – How to fill ComboBox with a description of objects inside an ArrayList?

Question:

I have an ArrayList of objects, which I am adding one by one as I need to register ( Insumos ). These objects have a description (name).

I need to fill a ComboBox with the descriptions of said objects within the ArrayList .

My code is:

ComboBox<String> combito new = ComboBox<String>();
DefaultComboBoxModel modelito = new DefaultComboBoxModel();
combito.setModel(modelito);

And here comes the doubt. Should I loop through the entire Array of objects and use my toString() method to fill the combo?

for i to (final array) 
    modelito.add(toString(ArrayList[i]))

Is that so?

Answer:

You simply iterate over your Array of Objects

for(Objeto objeto : ArrayObjetos) {
    combito.addItem(objeto.toString());
}

But it seems to me that what you want is to extract some property to add it to the ComboBox and not add the object as String , for example if your object had a name field, and it has a getNombre() method, which obtains the value as String , already You wouldn't need to use toString() :

for(Objeto objeto : ArrayObjetos) {
    combito.addItem(objeto.getNombre());
}
Scroll to Top