如何使用ArrayList填充JComboBox?

时间:2009-08-18 03:55:13

标签: java swing arraylist jcombobox

我需要使用ArrayList填充JComboBox。有没有办法做到这一点?

8 个答案:

答案 0 :(得分:22)

使用ArrayList类的toArray()方法并将其传递给JComboBox

的构造函数

有关详细信息,请参阅JavaDoctutorial

答案 1 :(得分:16)

使用 数组列表 填充 组合框 的优雅方式:

List<String> ls = new ArrayList<String>(); 
jComboBox.setModel(new DefaultComboBoxModel(ls.toArray()));

答案 2 :(得分:9)

我不喜欢接受的答案或@ fivetwentysix关于如何解决这个问题的评论。它有一种方法可以做到这一点,但并没有给出使用toArray的完整解决方案。你需要使用toArray并给它一个参数,这个参数是一个正确类型和大小的数组,这样你就不会得到一个Object数组。虽然对象阵列可以工作,但我并不认为它是强类型语言的最佳实践。

String[] array = arrayList.toArray(new String[arrayList.size()]);
JComboBox comboBox = new JComboBox(array);

或者,您也可以通过使用for循环来保持强类型。

String[] array = new String[arrayList.size()];
for(int i = 0; i < array.length; i++) {
    array[i] = arrayList.get(i);
}
JComboBox comboBox = new JComboBox(array);

答案 3 :(得分:3)

我相信您可以使用ArrayList创建一个新的Vector并将其传递给JCombobox构造函数。

SELECT      DATE(creacion) as fecha,
            SUM(case when order_paid = 'Approved' and info_direccion_pais = 'GB' then 1 else 0 end) as sales_UK,
            SUM(case when order_paid = 'Approved' and info_direccion_pais = 'AU' then 1 else 0 end) as sales_AU
FROM        orders
WHERE       DATE(creacion) >= '2015-01-01'
GROUP BY    fecha
ORDER BY    fecha DESC
LIMIT       0, 100

我的例子只是字符串。

答案 4 :(得分:3)

DefaultComboBoxModel dml= new DefaultComboBoxModel();
    for (int i = 0; i < <ArrayList>.size(); i++) {
        dml.addElement(<ArrayList>.get(i).getField());
    }

    <ComboBoxName>.setModel(dml);

可理解的代码。编辑&lt;&gt;根据需要。

答案 5 :(得分:1)

检查此简单代码

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

    }

}

答案 6 :(得分:1)

通过组合现有答案(this onethis one),向ArrayList添加JComboBox的正确类型安全方法如下:

private DefaultComboBoxModel<YourClass> getComboBoxModel(List<YourClass> yourClassList)
{
    YourClass[] comboBoxModel = yourClassList.toArray(new YourClass[0]);
    return new DefaultComboBoxModel<>(comboBoxModel);
}

在您的GUI代码中,将整个列表设置为JComboBox,如下所示:

DefaultComboBoxModel<YourClass> comboBoxModel = getComboBoxModel(yourClassList);
comboBox.setModel(comboBoxModel);

答案 7 :(得分:-4)

我认为这是解决方案

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