28

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

kleopatra
  • 51,061
  • 28
  • 99
  • 211
Abdul Khaliq
  • 2,423
  • 12
  • 40
  • 65

8 Answers8

26

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.

eric.christensen
  • 3,191
  • 4
  • 29
  • 35
  • 3
    If 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
  • 2
    in 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
25

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])));
Manawyddan
  • 15
  • 6
Ivan Aracki
  • 4,861
  • 11
  • 59
  • 73
13

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);
sage88
  • 4,104
  • 4
  • 31
  • 41
4
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.

Athif Shaffy
  • 692
  • 3
  • 10
  • 22
3

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.

stevoblevo
  • 78
  • 7
3

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

    }

}
Draken
  • 3,134
  • 13
  • 34
  • 54
k_kumar
  • 201
  • 1
  • 14
1

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);
BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
-5

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());
  • 2
    This 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