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


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();

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) 

Is that so?


You simply iterate over your Array of Objects

for(Objeto objeto : ArrayObjetos) {

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) {
Scroll to Top