使用 netbeans 将 ArrayList 加载到 JCombobox [英] Loading an ArrayList into a JCombobox using netbeans
问题描述
目前有一个名为 SundayList 的 ArrayList,它会在框架 AddStudent 加载后立即加载(GUI 部分)
Currently have an ArrayList called SundayList which is loaded as soon as the frame AddStudent is loaded (bit of GUI)
Netbeans 自动生成的代码为:
The code automatically generated by Netbeans is:
comboboxSunday = new javax.swing.JComboBox();
comboboxSunday.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item1", "Item2" }));
如何使用我自己的数组加载组合框项目?该数组包括以下项目:
How do I load the combobox items with my own array? The array includes items such as:
Activity1
Activity2
Activity3
Activity4
在我之前的搜索中,人们提到使用 toString()
和 toArray()
,但我不熟悉这两种方法,以及它们如何帮助将列表加载到组合框中,因为我对 java 很陌生..
From my previous search, people mentioned about using a toString()
and toArray()
, and I'm not familiar with either methods, and how they help in loading the list into the combobox as I'm quite new to java..
推荐答案
您可以创建自己的 ComboBoxModel
以 List
作为主要参数,但这有点更多参与
You could create your own ComboBoxModel
that takes a List
as the main parameter, but that's a little more involved
comboboxSunday.setModel(new DefaultComboBoxModel());
for (Object item : listOfItems) {
comboboxSunday.addItem(item);
}
这篇关于使用 netbeans 将 ArrayList 加载到 JCombobox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!