简体   繁体   English

如何使用 ArrayList 填充 JComboBox?

[英]How do I populate a JComboBox with an ArrayList?

I need to populate a JComboBox with an ArrayList.我需要用 ArrayList 填充 JComboBox。 Is there any way to do this?有没有办法做到这一点?

Use the toArray() method of the ArrayList class and pass it into the constructor of the JComboBox使用ArrayList类的toArray()方法,传入JComboBox的构造函数中

See the JavaDoc and tutorial for more info.有关更多信息,请参阅JavaDoc教程

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

I don't like the accepted answer or @fivetwentysix's comment regarding how to solve this.我不喜欢接受的答案或@fivetwentysix 关于如何解决这个问题的评论。 It gets at one method for doing this, but doesn't give the full solution to using toArray.它有一种方法可以做到这一点,但没有给出使用 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.您需要使用 toArray 并为其提供一个参数,该参数是一个正确类型和大小的数组,这样您就不会得到一个 Object 数组。 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.或者,您也可以仅使用 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);
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.可理解的代码。根据需要使用类型编辑<>

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.我的例子只是字符串。

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:通过结合现有答案(这个这个),将ArrayList添加到JComboBox的正确类型安全方法如下:

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:在您的GUI代码中,您将整个列表设置到JComboBox ,如下所示:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM