我需要用ArrayList填充JComboBox。有没有办法做到这一点?如何用ArrayList填充JComboBox?
回答
以供将来参考:
//first create the array;
String[] comboBoxArray = {"item1","item2","item3"};
//create JComboBox and assign it to the comboBox
JComboBox comboBox1 = new JComboBox(comboBoxArray);
这根本不回答这个问题。他想知道如何使用ArrayList来完成,而不是使用原始类型数组。 – sage88 2014-10-13 19:21:45
我认为这是解决
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行代码解决,而不需要所有这些混乱。 – sage88 2014-10-13 19:53:30
我不喜欢关于如何解决这一公认的答案或@ 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);
优雅的方式来填补组合框与数组列表:
List<String> ls = new ArrayList<String>();
jComboBox.setModel(new DefaultComboBoxModel(ls.toArray()));
我还没有试过这个,但是好像你最终会用一个Object数组填充jComboBox而不是使用这种方法的String数组。 – sage88 2016-06-03 02:16:54
我相信你可以使用ArrayList中创建一个新的载体,并传递到JComboBox中构造函数。
JComboBox<String> combobox = new JComboBox<String>(new Vector<String>(myArrayList));
我的例子只是字符串,虽然。
DefaultComboBoxModel DLM = new DefaultComboBoxModel();
for (int i = 0; i < <ArrayList>.size(); i++) {
DLM.addElement(<ArrayList>.get(i).getField());
}
<ComboBoxName>.setModel(DLM);
易懂的代码。编辑<>根据需要。
- 1. 用不同类的ArrayList填充JComboBox
- 2. 如何从Arraylist填充AlertDialog?
- 3. 如何从文本文件填充JComboBox?
- 4. 填充的ArrayList
- 5. 如何用填充了Arraylist对象的对象填充Listview
- 6. 从Enum填充Swing JComboBox
- 7. 用hashmaps的ArrayList填充ExpandableList
- 8. 用ArrayList填充JavaFX按钮
- 9. 用for循环填充ArrayList
- 10. 使用getter/setters填充JComboBox的问题
- 11. 用数据库信息填充jcombobox
- 12. 使用枚举类填充JComboBox
- 13. 无法填充ArrayList
- 14. 如何在Clojure中填充ArrayList?
- 15. 如何在Android中从ArrayList填充listview?
- 16. 如何使用mySQL数据库中的信息填充jcombobox?
- 17. from ArrayList into JComboBox
- 18. 在Netbeans 6.5中填充JAVA中的jComboBox
- 19. 填充的JTable从ArrayList中
- 20. 的Java 8 - 填充的ArrayList
- 21. 用对象填充ArrayList与相同的值填充
- 22. 如何在填充JComboBox的时候打开弹出菜单?
- 23. 如何从ArrayList更新JComboBox内容?
- 24. 重新填充的ArrayList使用Struts 2
- 25. 使用arraylist填充java中的JTable
- 26. 使用ArrayList填充mysql数据库行
- 27. 使用位图ArrayList动态填充ListView
- 28. 使用GregorianCalendar日期对象填充ArrayList
- 29. 如何通过ArrayAdapter使用ArrayList填充微调器?
- 30. Android的 - 如何用jsoup解析HTML并填充到ArrayList中?
如果你正在做一些像ArrayList。在你的Person类中,你可以定义toString()来调整ComboBox的值。另外,当使用ArrayList.toArray()时,你可能不得不声明你的数组变量为Object [](而不是String [])。 –
fivetwentysix
2011-05-12 03:19:43
在教程中没有示例与arraylist – Lobato 2015-01-09 20:29:11