I need to populate a JComboBox with an ArrayList. Is there any way to do this?
8 Answers
Use the toArray()
method of the ArrayList class and pass it into the constructor of the JComboBox

- 3,191
- 4
- 29
- 35
-
3If you're doing something like ArrayList
. in your Person class, you can define toString() which will adjust what your value is for the ComboBox. Also, you may have to declare your array variable as Object[] (instead of String[]) when using ArrayList.toArray(). – fivetwentysix May 12 '11 at 03:19 -
2in the tutorial there are no examples with an arraylist – Rafael Jan 09 '15 at 20:29
-
for objects you can use Java streams API. List
list = valyes.stream().map(o.field).collect(Collectors.toList()); – Sree Sep 27 '20 at 11:21
Elegant way to fill combo box with an array list :
List<String> ls = new ArrayList<String>();
jComboBox.setModel(new DefaultComboBoxModel<String>(ls.toArray(new String[0])));

- 15
- 6

- 4,861
- 11
- 59
- 73
-
2I've not tried this, but it seems like you'd end up filling the jComboBox with an Object array rather than a String array using this approach. – sage88 Jun 03 '16 at 02:16
-
2
I don't like the accepted answer or @fivetwentysix's comment regarding how to solve this. It gets at one method for doing this, but doesn't give the full solution to using toArray. You need to use toArray and give it an argument that's an array of the correct type and size so that you don't end up with an Object array. While an object array will work, I don't think it's best practice in a strongly typed language.
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.
String[] array = new String[arrayList.size()];
for(int i = 0; i < array.length; i++) {
array[i] = arrayList.get(i);
}
JComboBox comboBox = new JComboBox(array);

- 4,104
- 4
- 31
- 41
DefaultComboBoxModel dml= new DefaultComboBoxModel();
for (int i = 0; i < <ArrayList>.size(); i++) {
dml.addElement(<ArrayList>.get(i).getField());
}
<ComboBoxName>.setModel(dml);
Understandable code.Edit<>
with type as required.

- 692
- 3
- 10
- 22
I believe you can create a new Vector using your ArrayList and pass that to the JCombobox Constructor.
JComboBox<String> combobox = new JComboBox<String>(new Vector<String>(myArrayList));
my example is only strings though.

- 78
- 7
Check this simple code
import java.util.ArrayList;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class FirstFrame extends JFrame{
static JComboBox<ArrayList> mycombo;
FirstFrame()
{
this.setSize(600,500);
this.setTitle("My combo");
this.setLayout(null);
ArrayList<String> names=new ArrayList<String>();
names.add("jessy");
names.add("albert");
names.add("grace");
mycombo=new JComboBox(names.toArray());
mycombo.setBounds(60,32,200,50);
this.add(mycombo);
this.setVisible(true); // window visible
}
public static void main(String[] args) {
FirstFrame frame=new FirstFrame();
}
}
By combining existing answers (this one and this one) the proper type safe way to add an ArrayList
to a JComboBox
is the following:
private DefaultComboBoxModel<YourClass> getComboBoxModel(List<YourClass> yourClassList)
{
YourClass[] comboBoxModel = yourClassList.toArray(new YourClass[0]);
return new DefaultComboBoxModel<>(comboBoxModel);
}
In your GUI
code you set the entire list into your JComboBox
as follows:
DefaultComboBoxModel<YourClass> comboBoxModel = getComboBoxModel(yourClassList);
comboBox.setModel(comboBoxModel);

- 17,329
- 10
- 113
- 185
i think that is the solution
ArrayList<table> libel = new ArrayList<table>();
try {
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session s = sf.openSession();
s.beginTransaction();
String hql = "FROM table ";
org.hibernate.Query query = s.createQuery(hql);
libel= (ArrayList<table>) query.list();
Iterator it = libel.iterator();
while(it.hasNext()) {
table cat = (table) it.next();
cat.getLibCat();//table colonm getter
combobox.addItem(cat.getLibCat());
}
s.getTransaction().commit();
s.close();
sf.close();
} catch (Exception e) {
System.out.println("Exception in getSelectedData::"+e.getMessage());

- 1
- 1
-
2This is incredibly confusing. This problem can be solved with 2 lines of code without need for all of this mess. – sage88 Oct 13 '14 at 19:53