Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I populate a JComboBox with an ArrayList?

I need to populate a JComboBox with an ArrayList. Is there any way to do this?

like image 796
Abdul Khaliq Avatar asked Aug 18 '09 03:08

Abdul Khaliq


People also ask

How do you populate a JComboBox from an ArrayList?

String[] array = arrayList. toArray(new String[arrayList. size()]); JComboBox comboBox = new JComboBox(array); Alternatively, you can also maintain strong typing by just using a for loop.

How do you populate a comboBox in Java?

ArrayList<String> a = new ArrayList<String>(); a. add(0, "hahah"); a. add(1, "bleeeee"); a. add(5, "cleeeee"); //this makes an error, when I change index to 2, it works JComboBox supplierComboBox = new JComboBox(a.

DO FOR EACH loops work with Arraylists?

The enhanced for loop (sometimes called a "for each" loop) can be used with any class that implements the Iterable interface, such as ArrayList .


1 Answers

Use the toArray() method of the ArrayList class and pass it into the constructor of the JComboBox

See the JavaDoc and tutorial for more info.

like image 143
eric.christensen Avatar answered Sep 21 '22 22:09

eric.christensen