Java 如何使用 ArrayList 填充 JComboBox?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1291704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 07:50:03  来源:igfitidea点击:

How do I populate a JComboBox with an ArrayList?

javaswingarraylistjcombobox

提问by Abdul Khaliq

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

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

采纳答案by eric.christensen

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

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

See the JavaDocand tutorialfor more info.

有关更多信息,请参阅JavaDoc教程

回答by user3468789

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

回答by sage88

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.

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

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.

或者,您也可以仅使用 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);

回答by Ivan Aracki

Elegant way to fill combo boxwith an array list:

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

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

回答by stevoblevo

I believe you can create a new Vector using your ArrayList and pass that to the JCombobox Constructor.

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

JComboBox<String> combobox = new JComboBox<String>(new Vector<String>(myArrayList));

my example is only strings though.

我的例子只是字符串。

回答by Athif Shaffy

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.

可理解的代码<>。根据需要编辑类型。

回答by k_kumar

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 BullyWiiPlaza

By combining existing answers (this oneand this one) the proper type safe way to add an ArrayListto a JComboBoxis the following:

通过结合现有答案(this onethis one),将ArrayLista添加到 a的正确类型安全方法JComboBox如下:

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

In your GUIcode you set the entire list into your JComboBoxas follows:

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

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