I need to populate a JComboBox with an ArrayList. Is there any way to do this?
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.
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.
The enhanced for loop (sometimes called a "for each" loop) can be used with any class that implements the Iterable interface, such as ArrayList .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With